caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Radu Grigore <radugrigore@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] assertions or exceptions?
Date: Thu, 15 Jul 2004 15:49:19 +0300	[thread overview]
Message-ID: <7f8e92aa04071505495315454@mail.gmail.com> (raw)
In-Reply-To: <20040715101826.GA10062@redhat.com>

On Thu, 15 Jul 2004 11:18:26 +0100, Richard Jones <rich@annexia.org> 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:

http://blogs.msdn.com/cyrusn/archive/2004/06/16/156865.aspx

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).

regards,
 radu

-------------------
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


  parent reply	other threads:[~2004-07-15 12:49 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-15  8:03 Radu Grigore
2004-07-15 10:18 ` Richard Jones
2004-07-15 10:28   ` Daniel Andor
2004-07-15 12:49   ` Radu Grigore [this message]
2004-07-15 13:33     ` Richard Jones
2004-07-15 13:58       ` Radu Grigore
2004-07-16 18:53         ` Aleksey Nogin
2004-07-17  2:55           ` John Prevost
2004-07-17 14:24             ` David MENTRE
2004-07-15 12:35 ` Jon Harrop
2004-07-15 13:45   ` Radu Grigore
2004-07-15 14:33     ` Jon Harrop
2004-07-15 15:05       ` Radu Grigore
2004-07-15 16:24     ` skaller
2004-07-15 15:38 ` [Caml-list] Unboxing options, was " Brian Hurt
2004-07-15 16:25   ` John Hughes
2004-07-15 17:00     ` Brian Hurt
2004-07-15 17:20   ` John Prevost
2004-07-15 19:14     ` Radu Grigore
2004-07-15 19:56     ` John Carr
2004-07-15 20:48       ` Brian Hurt
2004-07-15 20:49         ` John Carr
2004-07-15 21:15           ` John Prevost
2004-07-15 21:15           ` Karl Zilles
2004-07-15 21:26           ` Brian Hurt
2004-07-15 21:04       ` John Prevost
2004-07-15 21:17     ` skaller
2004-07-15 21:35       ` Brian Hurt
2004-07-15 21:51         ` skaller
2004-07-15 21:42       ` skaller
2004-07-16  0:35     ` Jacques GARRIGUE
2004-07-16  1:03       ` John Prevost
2004-07-16  2:00         ` Jacques GARRIGUE
2004-07-16 16:40         ` Xavier Leroy
2004-07-19  8:58           ` Damien Doligez

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=7f8e92aa04071505495315454@mail.gmail.com \
    --to=radugrigore@gmail.com \
    --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).