Skip to content

Instantly share code, notes, and snippets.

@mausch
Created July 27, 2012 14:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mausch/3188428 to your computer and use it in GitHub Desktop.
Save mausch/3188428 to your computer and use it in GitHub Desktop.
Async exception handling in F#
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