From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p8FAZaHR008742 for ; Thu, 15 Sep 2011 12:35:36 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjsDAITUcU5ZELGagWdsb2JhbABCp2EBARYmJoFTAQEEASdFCgMFCwsYLlcGE4d3ArVGhhRgBJNHkSs X-IronPort-AV: E=Sophos;i="4.68,386,1312149600"; d="scan'208";a="109163386" Received: from recoil.dh.bytemark.co.uk (HELO dark.recoil.org) ([89.16.177.154]) by mail4-smtp-sop.national.inria.fr with SMTP; 15 Sep 2011 12:35:33 +0200 Received: (qmail 7919 invoked by uid 634); 15 Sep 2011 10:35:33 -0000 X-Spam-Level: * X-Spam-Check-By: dark.recoil.org Received: from no-dns-yet.demon.co.uk (HELO [192.168.14.100]) (62.49.66.12) (smtp-auth username remote@recoil.org, mechanism cram-md5) by dark.recoil.org (qpsmtpd/0.83) with ESMTPA; Thu, 15 Sep 2011 11:35:32 +0100 Mime-Version: 1.0 (Apple Message framework v1244.3) Content-Type: text/plain; charset=windows-1252 From: Anil Madhavapeddy In-Reply-To: <4E71CDB8.5020704@dogguy.org> Date: Thu, 15 Sep 2011 11:35:32 +0100 Cc: caml-list@inria.fr Message-Id: References: <20110913183714.GA15241@yeeloong.happyleptic.org> <4E71CDB8.5020704@dogguy.org> To: Mehdi Dogguy X-Mailer: Apple Mail (2.1244.3) X-Virus-Checked: Checked by ClamAV on dark.recoil.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id p8FAZaHR008742 Subject: Re: [Caml-list] Lwt and exceptions 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