From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 5C0C2BBAF for ; Mon, 31 May 2010 16:36:24 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmsCAC9mA0zZSMDqkWdsb2JhbACeMxUBAQEBBw0KBxEDH8EVhRYE X-IronPort-AV: E=Sophos;i="4.53,334,1272837600"; d="scan'208";a="63701168" Received: from fmmailgate03.web.de ([217.72.192.234]) by mail4-smtp-sop.national.inria.fr with ESMTP; 31 May 2010 16:36:23 +0200 Received: from smtp04.web.de ( [172.20.0.225]) by fmmailgate03.web.de (Postfix) with ESMTP id 4B166153EDFC1; Mon, 31 May 2010 16:36:23 +0200 (CEST) Received: from [78.43.204.177] (helo=frosties.localdomain) by smtp04.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #4) id 1OJ66J-0001PW-00; Mon, 31 May 2010 16:36:23 +0200 Received: from mrvn by frosties.localdomain with local (Exim 4.71) (envelope-from ) id 1OJ66I-0000rC-NU; Mon, 31 May 2010 16:36:22 +0200 From: Goswin von Brederlow To: Richard Jones Cc: Hans Ole Rafaelsen , caml-list@inria.fr Subject: Re: [Caml-list] Static exception analysis or alternative to using exceptions References: <20100527170122.GA28273@annexia.org> Date: Mon, 31 May 2010 16:36:22 +0200 In-Reply-To: <20100527170122.GA28273@annexia.org> (Richard Jones's message of "Thu, 27 May 2010 18:01:22 +0100") Message-ID: <87y6f0cf4p.fsf@frosties.localdomain> User-Agent: Gnus/5.110009 (No Gnus v0.9) XEmacs/21.4.22 (linux, no MULE) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX18zHXw34Y/BRH9Hties74Zz7g9L0a6C8ee4bIaX ScXJJS1TdCjtjgHzPC2xccRRTDs2u5lRsriQcbaK1FcDVPpKFU KYORbndgg= X-Spam: no; 0.00; 0200,:01 rafaelsen:01 monads:01 stdlib:01 complicates:01 ocaml:01 lacks:01 impl:01 'open:01 'open:01 val:01 val:01 26,:98 mfg:98 imho:01 Richard Jones writes: > On Wed, May 26, 2010 at 06:15:05PM +0200, Hans Ole Rafaelsen wrote: >> What experience does people have to using alternatives to exceptions, such >> as option types or exception monads? Does use of third part libraries that >> still throws exceptions make such approaches hard to use? Performance wise >> it seems to be comparable to catching exceptions or matching for options, so >> I guess the difference be might a question of programming style? > > Personally I've found that you should only throw those exceptions > which can be caught in a single place in the program. By this I mean > that an exception such as Not_found shouldn't be thrown, and instead > it would be better to use an option type (for stdlib functions which > throw Not_found, you have to be _very_ careful that the exception > cannot "escape"). Which needlessly complicates your code when it never happens. Imho a good module should provide both an exception and option based interface to fit the circumstances and programming style. > However if the exception is, say, an I/O error reading a disk file, > these should be thrown, and caught somewhere central where you can > display an error message to the user (for GUI programs) or abort the > current transaction (for server programs). Recovering from such > exceptions properly is still tricky though. Since OCaml lacks > 'finally', you either have to use a 'finally' impl from a library, or > modify your code to not need it (eg. turning calls to 'open_in' and > 'open_out' into a kind of continuation-passing style). Or for small > programs, abort the program and don't deal with recovery at all. > > All in all, this is not ideal for writing correct programs. Some sort > of exception analysis would be most welcome. It would be nice if the possible exceptions of a function would be part of the type. E.g. let f1 () = raise Not_found val f1 : unit -> 'a [ Not_found ] let f2 () = try f1 () with Not_found -> () val f2 : unit -> unit let f3 f = try f () with Not_found -> () val f3: (unit -> 'a [< Not_found | 'B ]) -> 'a [ 'B ] and so on. Someone would have to write a new type system for that though. MfG Goswin