我已經寫了一個函數,在F#中給出url和WebClient對象的情況下啓動下載。然而,當我寫我的嘗試..與聲明是給我的錯誤「不完整的結構構造在表達或此點之前」。使用WebClient對象進行異常處理
let urlDownload(url:string, webClient:WebClient) =
async {
try
let uri = new Uri(url)
/// References for progress queries
let contentLength = ref 0L
let bytesReceived = ref 0L
/// Updates progress statistics as progress is made
webClient.DownloadProgressChanged.Add(
fun args ->
if !contentLength = 0L && webClient.ResponseHeaders.Get "Content-Length" <> null then
contentLength := webClient.ResponseHeaders.Get "Content-Length" |> Int64.Parse
bytesReceived := !bytesReceived + args.BytesReceived
)
let! html = webClient.AsyncDownloadString(uri)
with
| :? UriFormatException -> printfn "Invalid URL"
}
它是基於斷碼的從MSDN here
什麼是真正奇怪的是,如果我把「printfn‘’」前有塊項目順利完成編譯。但是,當我運行它時,它會拋出一個UriFormatException異常,它被認爲是被with塊捕獲的。