13

I am trying to write non-blocking code in F#. I need to download a webpage, but sometime that webpage doesn't exist and an exception is thrown (404 Not Found) by AsyncDownloadString. I tried the code below but it doesn't compile.

How could I handle exception from AsyncDownloadString?

let downloadPage(url: System.Uri) = async {
 try
 use webClient = new System.Net.WebClient()
 return! webClient.AsyncDownloadString(url)
 with error -> "Error"
}

How am I suppose to handle exception here? If an error is thrown, I simply want to return an empty string or a string with a message in it.

Jack P.
11.5k1 gold badge31 silver badges34 bronze badges
asked May 20, 2013 at 14:42

1 Answer 1

22

Just add the return keyword when you return your error string:

let downloadPage(url: System.Uri) = async {
 try
 use webClient = new System.Net.WebClient()
 return! webClient.AsyncDownloadString(url)
 with error -> return "Error"
}

IMO a better approach would be to use Async.Catch instead of returning an error string:

let downloadPageImpl (url: System.Uri) = async {
 use webClient = new System.Net.WebClient()
 return! webClient.AsyncDownloadString(url)
}
let downloadPage url =
 Async.Catch (downloadPageImpl url)
answered May 20, 2013 at 14:51
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Jack! It works! :) Why do you think Async.Catch is a better approach. I would think that exception handling should be done in downloadPage, no?
I think Async.Catch is better because: (1) it preserves information about the error...there are other reasons an exception might be thrown besides a 404, and having the exception instead of "Error" makes it easier to diagnose the problem; (2) using Choice<_,_> allows you to use the type system to enforce the processing paths for both the results and the errors.
There are circumstances where Async.Catch can "leak" an exception (for example when an exception is thrown before the async is created - see: gist.github.com/jwosty/c6cb8ae1111ccc5caeb0f6db591d879e). Using a try-with block catches that exception.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.