On 15 Sep 2011, at 11:04, Mehdi Dogguy wrote:

> On 13/09/2011 20:37, [email protected] wrote:
>> The Lwt doc states that you should not use "raise" when using Lwt but 
>> use Lwt.fail instead.
>> 
>> So, is it still OK to call functions (for instance from the stdlib) 
>> that may raise an exception, provided we catch it soon enough ? And by 
>> "soon enough" I mean: before an Lwt call that could schedule another 
>> thread.
>> 
> 
> I guess, not (and it has been answered already). In fact, I was wondering
> if Lwt's authors would be against adding a function like:
> 
>       let wrap f x = try Lwt.return (f x) with e -> Lwt.fail e
> 
> It is stupid, trivial, etc… but looks what we need most of the time, no?
> Instead of doing it in our own code, it could land in Lwt directly.
> But, if it gets integrated into Lwt proper, users should be warned about
> its behaviour. (especially with impure functions).

The try_lwt construct (in pa_lwt) or try_bind already converts normal 
exceptions into Lwt ones:

  try_lwt
    raise (Failure "")
  with Failure _ ->
    print_endline "Fail"; Lwt.return ()

...will print "Fail".

You just need to careful about raising exceptions across yield points when a 
try_lwt isn't present. The slightly painful thing about converting existing 
code to Lwt is that any function that raises an exception will gain the Lwt 
type if you convert it to use try_lwt/catch, even if the rest of the code 
doesn't otherwise block.

Anil



-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to