From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: "Michaël Le Barbier" <michael.le_barbier@laposte.net>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Preferred use of Invalid_argument and Failure
Date: Wed, 24 Oct 2007 10:06:56 +0200 [thread overview]
Message-ID: <471EFD20.5010303@inria.fr> (raw)
In-Reply-To: <867ildqacq.fsf@Llea.celt.neu>
> 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
next prev parent reply other threads:[~2007-10-24 8:06 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-24 6:45 Michaël Le Barbier
2007-10-24 7:06 ` [Caml-list] " Alain Frisch
2007-10-24 8:06 ` Xavier Leroy [this message]
2007-10-24 11:05 ` Yaron Minsky
2007-10-24 11:30 ` Joel Reymont
2007-10-24 13:15 ` Yaron Minsky
2007-10-24 13:22 ` Daniel Bünzli
2007-10-24 14:45 ` Jon Harrop
2007-10-24 15:10 ` Richard Jones
2007-10-24 14:44 ` Jon Harrop
2007-10-25 7:07 ` Michaël Le Barbier
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=471EFD20.5010303@inria.fr \
--to=xavier.leroy@inria.fr \
--cc=caml-list@yquem.inria.fr \
--cc=michael.le_barbier@laposte.net \
/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).