Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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)
share|improve this answer
    
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? –  Martin May 20 '13 at 15:08
2  
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. –  Jack P. May 20 '13 at 15:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.