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 (12:49)
From: Radu Grigore <radugrigore@g...>
Subject: Re: [Caml-list] assertions or exceptions?
On Thu, 15 Jul 2004 11:18:26 +0100, Richard Jones <> wrote:
> [...]  But in
> many cases, the underlying C fields being NULL is an internal error
> for which we are not prepared, and the only sensible thing to do then
> is not to force people to deal with it locally, but to throw an
> exception which causes the whole script/program to fail.

You say that the only sensible thing to do is for the program to fail.
But that's what assertions (not exceptions) are for. So why don't you
assert when the C string is NULL? I see couple of reasons: (1) without
knowing the client code you can't be sure it's an internal error and
(2) you want to give the client a chance to fail gracefully (ie. save
any state that might be useful to the user, inform the user, etc.).

The first reason does not hold for internal/private functions, ie.
those functions that have clients which are under your control. Would
you use an assertion for such a function? Or you never use assertions?

Note that in your example there is always a third choice (the one
advocated by .NET class library design guidelines): do what you do but
also provide a function

   Request.valid_hostname : Request.t -> bool

You might argue that the end result is the same for the client. With
your solution a client that does not expect a failure does not write a
try block, while one that knows it's OK to fail from time to time will
use a try block. The problem is that you force the client to use an
exception in circumstances that are _not_ exceptional from his point
of view. So giving him the option of saying "if valid_hostname then I
don't expect this to fail" is a plus. See:

for another discussion along these lines.

BTW, I think that the solution you have chosen in this case is the
right one. I'm just trying to find some guidelines, which work most of
the time, of when to use an exception versus an assertion. The best
I've found so far is the advice from Sun to use exceptions for
checking preconditions of public methods and assertions everywhere
else. I'd go one step further and say that exceptions should be used
only in the public interface of a component (which is not the same as
a class -- especially for a language like OCaml :) ).

You said nothing about assertions. You don't use them?

> > 3. Is it possible to avoid using exceptions and read a text file
> > line-by-line until EOF?
> There are functions in ExtLib to do this, I think.  If not then you
> should write a function like 'input_all_lines' or 'iter_over_lines f'
> once, put it into a small local library, and use it every time.  No
> point reinventing wheels each time.

Ok, so I can hide the usage of the exception. It doesn't make me feel
much better. It feels more like redesigning the interface of the
standard library. (but it's a good idea nonetheless).


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