Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] assertions or exceptions?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-07-15 (13:45)
From: Radu Grigore <radugrigore@g...>
Subject: Re: [Caml-list] assertions or exceptions?
On Thu, 15 Jul 2004 13:35:22 +0100, Jon Harrop
<> wrote:

> [...] In
> OCaml, you often write in a functional style, when performing many similar
> operations this naturally lends itself to recursive functions. When running,
> a deeply recursed function may suddenly find a shortcut to the correct
> answer. Exceptions provide an ideal means to escape such deep recursions. 

This is an interesting idea. I'll try to spot such situations while coding!

> Convert the following code (which finds the product of a bunch of integers)
> into an equivalent which avoids exceptions but retains the efficiency of
> being able to bail earlier, upon encountering a zero:
> exception Zero
> let prod fold l =
>   try fold (fun a e -> if e=0 then raise Zero else a*e) 1 l
>   with Zero -> 0;;

All I can do is this:

let rec prod_fold a l = match a, l with
  | 0, _ -> 0
  | a, [] -> a
  | a, h :: t -> prod_fold (a * h) t

Differences between these are:
1. Yours makes an exception visible to other functions that do not use it
2. Yours is 80 non-ws characters long, mine is 71 characters long; so
they are comparable.
3. Mine is less flexible: it does not allow plugging in a fold
function. However I don't see where such a thing would be useful. An
4. I assert that mine is easier to understand. (at least for me :) )
One reason is that it doesn't use exceptions: only a simple recursive
5. I see no reason why performance would be significantly different.

> > Exceptions can be left uncaught and this is a potential cause
> > for a combinatorial explosion of the number of possible "execution
> > paths"...
> I would say it was poor style to allow the possible number of execution paths
> to explode.

You can use goto and keep the number of execution paths from exploding
too. This doesn't mean that it is not a dangerous tool and that you
should be careful while using it. The same applies to exceptions.

> As the above example shows, exceptions can be used to simplify code.

I'm not convinced.

> > 3. Is it possible to avoid using exceptions and read a text file
> > line-by-line until EOF?
> Yes (see other people's posts).

Those solutions are:
 1. use another library
 2. provide your own workaround (by redesigning the interface)
I fail to see how this vindicates the design of the standard library.

> > 4. When do you use assertions and when do you use exceptions?
> I use assertions to perform sanity checks.

You mean internal invariants? That is, including postconditions but
not preconditions?

> I use exceptions in two separate
> sitations: firstly to handle exceptional circumstances,

Tautologies do not contain a lot of information ;-) When is something
exceptional? When it shouldn't happen but you know what to do if it
does happen? If you remove the "but you know what to do.." part then
you are better of using an assertion.

> secondly to provide an escape route from computations.

As I said, this is an interesting idea but I really need to see it in
practice in order to understand it.


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: