From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 65DE7BC57 for ; Thu, 27 May 2010 19:01:24 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvsEAGtC/ktQRFuw/2dsb2JhbACeIHHCRYUTBA X-IronPort-AV: E=Sophos;i="4.53,312,1272837600"; d="scan'208";a="63534037" Received: from furbychan.cocan.org ([80.68.91.176]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/AES256-SHA; 27 May 2010 19:01:24 +0200 Received: from rich by furbychan.cocan.org with local (Exim 4.63) (envelope-from ) id 1OHgSQ-0008N4-Mo; Thu, 27 May 2010 18:01:22 +0100 Date: Thu, 27 May 2010 18:01:22 +0100 To: Hans Ole Rafaelsen Cc: caml-list@inria.fr Subject: Re: [Caml-list] Static exception analysis or alternative to using exceptions Message-ID: <20100527170122.GA28273@annexia.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.13 (2006-08-11) From: Richard Jones X-Spam: no; 0.00; 0200,:01 rafaelsen:01 monads:01 stdlib:01 ocaml:01 lacks:01 impl:01 'open:01 'open:01 26,:98 wrote:01 exception:01 exception:01 caml-list:01 functions:01 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"). 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. Rich. -- Richard Jones Red Hat