From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 5CF84BC69 for ; Wed, 24 Oct 2007 10:06:58 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAMaZHkfUGyokimdsb2JhbACOWQEBAQgEBhERBw X-IronPort-AV: E=Sophos;i="4.21,323,1188770400"; d="scan'208";a="5039430" Received: from smtp6-g19.free.fr ([212.27.42.36]) by mail3-smtp-sop.national.inria.fr with ESMTP; 24 Oct 2007 10:06:58 +0200 Received: from smtp6-g19.free.fr (localhost.localdomain [127.0.0.1]) by smtp6-g19.free.fr (Postfix) with ESMTP id AC0915FE34; Wed, 24 Oct 2007 10:06:57 +0200 (CEST) Received: from [192.168.1.3] (che78-2-82-237-71-191.fbx.proxad.net [82.237.71.191]) by smtp6-g19.free.fr (Postfix) with ESMTP id 31F925FD80; Wed, 24 Oct 2007 10:06:57 +0200 (CEST) Message-ID: <471EFD20.5010303@inria.fr> Date: Wed, 24 Oct 2007 10:06:56 +0200 From: Xavier Leroy User-Agent: Thunderbird 2.0.0.4 (X11/20070620) MIME-Version: 1.0 To: =?ISO-8859-1?Q?Micha=EBl_Le_Barbier?= Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Preferred use of Invalid_argument and Failure References: <867ildqacq.fsf@Llea.celt.neu> In-Reply-To: <867ildqacq.fsf@Llea.celt.neu> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; assertion:01 recovers:01 syntax:01 exception:01 exception:01 caml-list:01 functions:01 functions:01 exceptions:01 exceptions:01 undefined:01 alain:01 caml:02 caml:02 argument:02 > Let's quote the manual (release 3.09): > > exception Invalid_argument of string > > Exception raised by library functions to signal that the given > arguments do not make sense. > > exception Failure of string > > Exception raised by library functions to signal that they are > undefined on the given arguments. > > > It seems to me that Invalid_argument is a sort of specialisation of > Failure. The convention that the standard library tries to follow is this. Invalid_argument is very much like a failed assertion: it indicates that something is wrong in the program itself, i.e. negative character positions in string functions. Most programs will not catch Invalid_argument, treating as a fatal error. Others will catch it, but only to enter a piece of generic "recover from unexpected error" code. Failure, on the other hand, signals errors that can happen in normal runs of the code. For instance, you're converting a user-provided string to a number, and the string does not represent a number. It is expected that the client code catches Failure and recovers gracefully, e.g. by asking for the number again, or producing a precise "syntax error" message. I recommend the use of Invalid_argument to report "should never happen" conditions at the boundary between library functions and user code. On the other hand, the "Failure" exception is a bit of a legacy from earlier designs (Caml Light and even the original LeLisp-based Caml), and often is not the best way to report "normal error" conditions: instead, you could consider defining your own exceptions as Alain suggested, or even have your functions return "option" types instead of raising exceptions. Hope this helps, - Xavier Leroy