See code below for some pointers.

 

open System.IO
open System.Net

let httpAsync (url : string) = 
    async { 
        try
            let req = WebRequest.Create(url)    
            let! resp = req.AsyncGetResponse()

            let stream = resp.GetResponseStream()
            let reader = new StreamReader(stream)

            let contents = reader.ReadToEnd()
            return contents
        with e ->
            // when successful, the 'try' block returns an Async<string>,
            // so when exception that we catch, must also return 
            // Async<string>
            return sprintf "An Exception happened: %s" e.Message 
    }  

// Another common idiom is to have the function named with a 'try' prefix
// and return an option (return Some(result) for success, and None for
// failure)
let tryHttpAsync (url : string) = 
    async { 
        try
            let req = WebRequest.Create(url)    
            let! resp = req.AsyncGetResponse()

            let stream = resp.GetResponseStream()
            let reader = new StreamReader(stream)

            let contents = reader.ReadToEnd()
            return Some(contents)
        with e ->
            return None
    }  


Note that none of this is really related to 'async', this is all about how to handle exceptions in general, and it applies equally well to async.
By on 11/29/2009 9:42 AM ()
Thanks very much...

New language ,feel like an idiot.

Anyway I tried these options before

   return contents
  with 
////                //| _ -> failwith  "link failed" 
////                e -> printfn "%s" e.Message; "link failed" // |> ignore

but theses didnt work .. yet your similar code does . I was trying to return the string link failed do you have to have an explcit return ?



Also the code i pasted works as a script (.fsx) but fails as code (.fs)

I get this error

Error 1 Method or object constructor 'op_LessMinusMinus' not found C:\Users\Ben\Documents\Visual Studio 10\Projects\ConsoleApplication1\ConsoleApplication1\Program.fs 102 17 ConsoleApplication1
By on 11/29/2009 3:50 PM ()
The reason you need "return" there is because you're in a "computation expression" or "workflow" (inside the async { } part). When inside the async part, you need to return an 'a Async, not just any type such as a string. So you need to go from string to string Async. The special return keyword, available only inside comp-exprs, wraps your expression into the proper return type.

If a workflow ends with just some expression, the compiler tacks on a call to that workflow's "Zero" function, which returns a default/zero value. In the case of async, this is unit Async. So, when you have an async workflow that ends with a string, it's as if you wrote "someString; return ()". You can see that here:

> let x = async { "test" };;
let x = async { "test" };;
----------------^^^^^^
stdin(3,17): warning FS0020: This expression should have type 'unit', but has type 'string'. Use 'ignore' to discard the result of the expression, or 'let' to bind the result to a name.

val x : Async<unit>



By on 11/29/2009 5:04 PM ()
ty all
By on 11/29/2009 6:17 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us
Built with WebSharper