caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Anil Madhavapeddy <anil@recoil.org>
To: Mehdi Dogguy <mehdi@dogguy.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Lwt and exceptions
Date: Thu, 15 Sep 2011 11:35:32 +0100	[thread overview]
Message-ID: <E0A5DCCA-8092-480F-B3EB-82C45F22B5E5@recoil.org> (raw)
In-Reply-To: <4E71CDB8.5020704@dogguy.org>

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

> On 13/09/2011 20:37, rixed@happyleptic.org 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



  reply	other threads:[~2011-09-15 10:35 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-13 18:37 rixed
2011-09-13 19:17 ` Anil Madhavapeddy
2011-09-14 16:15   ` rixed
2011-09-15 12:24     ` Jérémie Dimino
2011-09-15 10:04 ` Mehdi Dogguy
2011-09-15 10:35   ` Anil Madhavapeddy [this message]
2011-09-15 12:09   ` Jérémie Dimino
2011-09-15 12:33     ` Mehdi Dogguy
2011-09-15 12:22   ` Dmitry Grebeniuk
2011-09-15 13:10     ` Jérémie Dimino
2011-09-17  6:38       ` Dmitry Grebeniuk
2011-09-17  9:23         ` Stéphane Glondu
2011-09-17 10:20         ` Jérémie Dimino

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E0A5DCCA-8092-480F-B3EB-82C45F22B5E5@recoil.org \
    --to=anil@recoil.org \
    --cc=caml-list@inria.fr \
    --cc=mehdi@dogguy.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).