caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: extlib <ocaml-lib-devel@lists.sourceforge.net>
Cc: caml-list <caml-list@inria.fr>
Subject: [Caml-list] Exceptions considered harmful
Date: 29 Jun 2004 11:02:49 +1000	[thread overview]
Message-ID: <1088470968.18587.123.camel@pelican.wigram> (raw)
In-Reply-To: <20040628173400.GB26193@fichte.ai.univie.ac.at>

On Tue, 2004-06-29 at 03:34, Markus Mottl wrote:
> On Tue, 29 Jun 2004, Martin Jambon wrote:
> > This makes me feel like we will not know if the "finally block" has been
> > completed or not. Why not totally prohibiting any exception raised from
> > the "finally block"?
> > 
> > let protect f x finally =
> >   let res =
> >     try f x
> >     with exc ->
> >       (try finally x with _ -> invalid_arg "protect")
> >       raise exc in
> >   (try finally x with _ -> invalid_arg "protect");
> >   res
> 
> But how do you know then that the exception came from "finally" and not
> from "f"?  You could even lose the information, whether "f" was executed
> successfully at all!

In general, I think exceptions are a bad idea.
I spend a lot of time removing them from my code :(

In a typical case it goes like this:

try 
  let f = open_in fname in
  do_something f;
  close_in f
with _ -> ()


Only this is very BAD code because it fails to isolate
two completely distinct possibilities for an error:
in opening the file, or in processing it: in the latter
case the file isn't closed: we didn't actually
expected a processing error here, and also fail to
correctly re-raise the exception.

This lack of localisation is quite hard to fix:

  let f = try open_in fname with _ -> () in
  do_something f;
  close_in f

WOOPS! that's a type error, which is good, because
we can't 'do_something' if the file wasn't opened.
What else can we try?

  try 
    let f = open_in fname in
    begin 
      try do_something f
      with _ -> close_in f 
    end;
    close_in f;
  with _ -> failwith "Do something failed"

WOOPS, we tried to close f twice. We can only do this:

  exception Some_error
  try 
    let f = open_in fname in
    begin 
      try do_something f
      with _ -> close_in f; raise Some_error
    end;
    close_in f;
   with Some_error -> raise Some_error
   | _ -> ()

but now we have lost the location and kind of the
error in something .. and we had to introduce a new
global exception as well. 

The obvious way to fix this mess is to eliminate the 
undesirable exceptions early:

  let result = 
    try Some (open_in fname) with _ -> None 
  in match result with
  | None -> ()
  | Some f -> 
    begin try do_something with x -> close_in f; raise x end
    close_in f

Of course, the result of the calculation here is unit,
more generally we'd be saying 

let result = do_something in close_in f; result

and obviously we'd be wrapping that as well.

Generally, the exceptions discard localisation
and destroy any kind of static assurance errors are handled.

To me, the Haskell monadic approach seems a sounder.

The problem with exceptions is that they're basically
without solid theoretical principles. Throwing out 
context is one thing .. recovering at an unknown
point, trapping unknown exceptions without any
aid from the type system simply defeats the purpose
of having a type system.

As far as I can see, just about the only way to
use exceptions properly is to eliminate them
at the earliest possible point .. which suggests
libraries simply shouldn't throw them.

It seems difficult to escape from deep recursions
without exceptions. They can be convenient.
They're useful for rapid prototyping where you
need a demo that works on some cases *fast*.

And they seem convenient when you 'know' there
can't be an error. The price is high. In avoiding
statically handling false negatives (errors that
can't occur) your code has become fragile: any change
which may in fact lead to raising an error will
not be discovered without testing.. and then it
will be very hard to find.

Would static exceptions be better? (Allow exceptions
but require that they're caught in an ancestor of the
scope they're raised in, and at least in a child of
the scope the exception is declared in)

Any comments on any of this appreciated.

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


       reply	other threads:[~2004-06-29  1:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20040628143917.GA21847@fichte.ai.univie.ac.at>
     [not found] ` <Pine.LNX.4.44.0406290056580.1229-100000@localhost>
     [not found]   ` <20040628173400.GB26193@fichte.ai.univie.ac.at>
2004-06-29  1:02     ` skaller [this message]
2004-07-04  7:30       ` Lauri Alanko
2004-07-04 20:16         ` Christophe TROESTLER
2004-07-04 20:24         ` Michael Hicks
2004-07-05  3:42           ` skaller
2004-07-11 13:12       ` [Caml-list] " Richard Cole
2004-06-29 18:05 [Caml-list] " Brandon J. Van Every

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=1088470968.18587.123.camel@pelican.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@inria.fr \
    --cc=ocaml-lib-devel@lists.sourceforge.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).