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: Mon, 2 Jun 2014 10:31:35 +0200	[thread overview]
Message-ID: <20140602083135.GA32010@frosties> (raw)
In-Reply-To: <244F680EFB184D4693C2F7AAC6889EAE@erratique.ch>

On Tue, May 27, 2014 at 11:39:20PM +0200, Daniel Bünzli wrote:
> Le mardi, 27 mai 2014 à 10:49, Goswin von Brederlow a écrit :
> > 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.
> 
> 
> Bad api. Neither exceptional error, neither programming error, *should* not raise Invalid_argument, of course you???ll have to handle it but don't forget to send hate emails to the API designer.  
> 
> > 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.
> 
> It???s not a matter of taste. What you are describing here is usage of exceptions by a programmer that wants to shoot himself in the foot. Libraries themselves should never disrupt the control flow of clients.  
> 
> Best,
> 
> Daniel

Who said anything about disrupting the codeflow of clients?

The use case is like

exception Result of int

let find_less_than x list =
  try
    List.iter (fun y -> if x > y then raise (Result y)) list;
    None
  with Result y -> Some y


# find_less_than 10 [100; 23; 3; 42; 17];;
- : int option = Some 3

You hrow the exception internally and catch it internally. Nothing of
the clients gets disruppted.

Obviously this is a constructed simplistic example. But in more
complex code an exception can be far more readable than 50 ifs and
checks of None/Some all over the place.

MfG
	Goswin

  reply	other threads:[~2014-06-02  8:31 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
2014-05-27  8:56           ` David House
2014-05-27 21:39           ` Daniel Bünzli
2014-06-02  8:31             ` Goswin von Brederlow [this message]
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=20140602083135.GA32010@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).