F#中的异常后继续循环

Ram*_*lló 1 .net f#

此代码用作示例,并显示异常.

open System.Net

let baseUrl = "http://fsharp.org/"
let someWords = ["learn"; "working"; "teaching"; "testimonials"] 

let downloadFile (url : string) (filePath : string) =
    use wc = new System.Net.WebClient()
    wc.DownloadFile(url, filePath)

for words in someWords do
    let joinedUrl =
        baseUrl
        + words.ToString()
    System.Console.WriteLine(joinedUrl)
    downloadFile joinedUrl (@"C:\temp\file" + words + ".txt")
Run Code Online (Sandbox Code Playgroud)

" http://fsharp.org/working "抛出' System.Net.WebException ',因为它不存在和for loop停止.我希望循环继续下一个字符串,在本例中为"teaching".

我试图处理异常try...with并添加reraise()但我还没有解决问题.

Dav*_*haw 5

尝试这样会吞下所有例外情况:

let downloadFile (url : string) (filePath : string) =
    try
        use wc = new System.Net.WebClient()
        wc.DownloadFile(url, filePath)
    with _ -> //Matches all exception types
        ()    //returns unit
Run Code Online (Sandbox Code Playgroud)

您可以更改此项以处理特定错误,或执行一些日志记录等.

另一个...如果你正在使用Visual Studio并将其配置为" Break on Exception ",你可能只是看到调试器在你的try..with中处理异常,你可以只是"F5"过去它,禁用调试中断,或从调试器中运行它.