comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Exceptions and out procedure arguments (using GNAT GPL)
Date: Wed, 20 Jun 2007 10:01:56 +0200
Date: 2007-06-20T09:59:06+02:00	[thread overview]
Message-ID: <1roxz9pqarixg.ykwxgyertwu2.dlg@40tude.net> (raw)
In-Reply-To: 1182288056.091791.248430@n15g2000prd.googlegroups.com

On Tue, 19 Jun 2007 14:20:56 -0700, Adam Beneschan wrote:

> The kind of thing
> I'd object to is, say, a procedure that reads a string from a file
> into an OUT parameter, and then raises an exception if the string
> doesn't conform to some syntax.  Then, from the caller's point of
> view, the procedure can *both* return valid (although inferior) output
> *and* raise an exception, which I think would make things difficult to
> understand for someone trying to read the code that calls the
> procedure---I'd be scratching my head trying to figure out why, after
> the caller has caught an exception in the procedure, is it still using
> the value returned by the procedure?  That's a case where an
> additional OUT parameter to say "this data is malformed" would be
> better.

Oh yes, this is a great example. No, I am using exceptions here. When
designing something like a recursive descent parser, I'm always use this
pattern:

   function Get (Source : Source_Type) return Thing;

when Get parses Thing (which can be as big as "package declarative region"
or "subprogram body" etc) it advances Source to the position following the
thing. When it fails, it propagates an exception and leaves Source in some
definite state, because the exception can potentially be handled to allow
compilation to continue and you cannot reasonably continue if you don't
know where you are. To use return codes for hundreds of Get's would be
extremely tedious and close to unreadable.

> There's a subtle difference between saying "a procedure that
> raises an exception shouldn't be counted on to produce valid output"
> and "a procedure that raises an exception shouldn't leave undefined
> random garbage lying around",

Yes

> but I realize that this is rather
> difficult for me to express precisely.

Actually I think that boils down to your stand point on exceptions vs.
program correctness. Mine is that any exception shall be a valid (correct)
outcome.

In other words, exceptions are a subject of DbC, they aren't a vehicle of.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  parent reply	other threads:[~2007-06-20  8:01 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-16  1:05 Exceptions and out procedure arguments (using GNAT GPL) Fionn Mac Cumhaill
2007-06-16  1:53 ` Anh Vo
2007-06-16  2:50 ` Brian May
2007-06-16  3:08 ` Randy Brukardt
2007-06-16  6:55 ` Dmitry A. Kazakov
2007-06-18 15:44 ` Adam Beneschan
2007-06-19  5:23   ` Fionn Mac Cumhaill
2007-06-19  7:34     ` Maciej Sobczak
2007-06-19 15:21       ` Adam Beneschan
2007-06-19 20:07         ` Dmitry A. Kazakov
2007-06-19 21:20           ` Adam Beneschan
2007-06-20  6:16             ` Georg Bauhaus
2007-06-20  8:01             ` Dmitry A. Kazakov [this message]
2007-06-20  8:45               ` Georg Bauhaus
2007-06-20  9:29                 ` Dmitry A. Kazakov
2007-06-20  6:21           ` Georg Bauhaus
2007-06-20  8:02             ` Dmitry A. Kazakov
2007-06-20  8:46               ` Georg Bauhaus
2007-06-20  9:29                 ` Dmitry A. Kazakov
2007-06-20 10:13                   ` Georg Bauhaus
2007-06-20 12:58                     ` Dmitry A. Kazakov
2007-06-20 14:16                       ` Georg Bauhaus
2007-06-20 18:22                         ` Dmitry A. Kazakov
2007-06-20 19:16                           ` Georg Bauhaus
2007-06-20 20:40                             ` Dmitry A. Kazakov
2007-06-21  9:52                               ` Georg Bauhaus
2007-06-21 13:48                                 ` Dmitry A. Kazakov
2007-06-22 18:15                                   ` Georg Bauhaus
2007-06-22 19:45                                     ` Dmitry A. Kazakov
2007-06-20 15:15         ` Fionn Mac Cumhaill
2007-06-19 21:40     ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox