-
-
Save lessismore1/10d0005baa6cad4ff2873435df008c21 to your computer and use it in GitHub Desktop.
Async exception handling in F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.Net | |
// exception handling in async using Async.Catch | |
let fetchAsync (name, url:string) = | |
async { | |
let uri = new System.Uri(url) | |
let webClient = new WebClient() | |
let! html = Async.Catch (webClient.AsyncDownloadString(uri)) | |
match html with | |
| Choice1Of2 html -> printfn "Read %d characters for %s" html.Length name | |
| Choice2Of2 error -> printfn "Error! %s" error.Message | |
} |> Async.Start | |
// exception handling in async using regular try/with | |
let fetchAsync2 (name, url:string) = | |
async { | |
let uri = new System.Uri(url) | |
let webClient = new WebClient() | |
try | |
let! html = webClient.AsyncDownloadString(uri) | |
printfn "Read %d characters for %s" html.Length name | |
with error -> printfn "Error! %s" error.Message | |
} |> Async.Start | |
fetchAsync2 ("blah", "http://asdlkajsdlj.com") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment