caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Uncaught exceptions in function type.
Date: Tue, 27 May 2014 10:49:54 +0200	[thread overview]
Message-ID: <20140527084954.GA15848@frosties> (raw)
In-Reply-To: <53B801AD6F5B4BFBA0DA2A69D8775497@erratique.ch>

On Mon, May 26, 2014 at 06:34:33PM +0200, Daniel Bünzli wrote:
> Le lundi, 26 mai 2014 à 18:02, Philippe Veber a écrit :
> > Thanks! BTW core still uses exceptions. Is there an explicit rule as to how to decide between Result type or exceptions. For instance, why not write the Array.create function like this:
> >  
> > val create : int -> 'a -> 'a array Or_error.t
> >  
> > where create fails for a negative integer?
> Because that would be utterly annoying. You need to make the
> following distinctions:
> 
> * Programming errors, for contracts with the programmer that cannot
> be enforced through types. For that raises Invalid_argument if the
> contract is violated. Invalid_argument is not supposed to be handled,
> it denotes an API misuse, like calling Array.create with a negative
> integer.

Sometimes arguments are computed and suddenly exceed aceptable
parameters. E.g. the user specifies a data file that needs to be read
into a string. If the file is too large you get Invalid_argument.
Programms can catch and handle such cases, at least to the point of
giving a good error message.

Idealy a program should catch all exceptions and explain in a human
readable way why they happened.
 
> * Exceptional errors, for errors that the programmer is unlikely to
> handle at all (e.g. out of memory). For that raise a custom exception.
> This should occur very rarely, you are unlikely to ever define one
> such exception.

Out of memory is probably the only exception you can't do anything
about in ocaml. Catching and handling it would nearly always need more
ram causing just another Ouf_of_memory exception. In ocaml you are
pretty much screwed.
 
> * Non-exceptional errors, errors that the programmer will have to
> handle (e.g. failing to connect a socket), for that do not use a
> custom exception but use variants or options types.  

That is a matter of taste and with recursions in the mix exceptions
can be a great way to abort the recursion while keeping the code flow
simple. They also allow you to use things like List.fold_left and
abort early.

Variants or option types are not always the best solution.
 
> In general if you write libraries it???s better to err on the side
> of exceptionless design: never use exceptions beyond Invalid_argument
> (and especially never use Not_found or Failure). Leave exception
> definition/usage at the discretion of the user (if he wishes to shoot
> himself in the foot).
> 
> Best,
> 
> Daniel

MfG
	Goswin

  parent reply	other threads:[~2014-05-27  8:49 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-26 14:23 Philippe Veber
2014-05-26 14:56 ` Romain Bardou
2014-05-26 15:13   ` Ben Millwood
2014-05-26 16:02     ` Philippe Veber
2014-05-26 16:34       ` Daniel Bünzli
2014-05-27  6:52         ` Philippe Veber
2014-05-27  8:42           ` Ben Millwood
2014-05-27 10:05             ` Goswin von Brederlow
2014-05-27 10:36               ` Ben Millwood
2014-05-27 11:24                 ` Yaron Minsky
2014-05-27 21:42             ` Daniel Bünzli
2014-05-27 21:16           ` Daniel Bünzli
2014-06-02  8:38             ` Goswin von Brederlow
2014-05-27  8:49         ` Goswin von Brederlow [this message]
2014-05-27  8:56           ` David House
2014-05-27 21:39           ` Daniel Bünzli
2014-06-02  8:31             ` Goswin von Brederlow
2014-05-27  9:25         ` Nicolas Boulay
2014-05-27 21:51           ` Daniel Bünzli
2014-05-30 18:03         ` Florian Weimer
2014-05-31 11:26           ` Daniel Bünzli
2014-06-02  8:43             ` Goswin von Brederlow
2014-05-26 15:25   ` Philippe Veber
2014-05-27  9:28     ` Goswin von Brederlow
2014-05-27  9:38       ` Romain Bardou
2014-05-26 15:33 ` Thomas Blanc
2014-05-26 16:04   ` Philippe Veber
2014-05-26 15:33 ` Gabriel Scherer

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=20140527084954.GA15848@frosties \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    /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).