From: Maciej Sobczak <no.spam@no.spam.com>
Subject: Re: Handling invalid objects
Date: Wed, 22 Mar 2006 17:42:53 +0100
Date: 2006-03-22T17:42:53+01:00 [thread overview]
Message-ID: <dvruqc$8j$1@sunnews.cern.ch> (raw)
In-Reply-To: <1c2j4h3onbhmk.p3uisoojg34b$.dlg@40tude.net>
Dmitry A. Kazakov wrote:
>>If by exception contracts you mean embedding the exception specification
>>in the "signature" of the procedure/function, then it was already
>>exercised by the Java community with rather disappointing effects.
>
> If Java did it wrong, let's do it right in Ada.
Do what exactly? This is important question.
The problem with exception specifications is that they are
self-contradictory:
- We use exceptions when we want to *DECOUPLE* error reporting from
error handling. We find it especially good in those situations, where
error reporting site and error handling site are separated by more than
one level of subroutine calls (otherwise returning error codes is good
enough).
- We embed contract information in subroutine signatures to *COUPLE* the
caller with the callee with respect to what they provide to each other
and what they expect from each other.
Now, "coupling" and "decoupling" are hardly compatible. Let's see where
it breaks in so-called practice.
First, there is a cascading effect when someone on one end of the chain
adds a new exception type. Just let's say that the project evolved and
for example a database got involved in something that was previously
managed with the use of files. There is a new DBError exception,
possibly having some db-specific information encoded (you know,
exceptions are real objects in some languages :) ). This error is not
handled neither by the offending function nor anybody in the chain, but
is supposed to be handled at some higher level. In this scheme, the poor
programmer has to add the DBError type to the exception specification to
*all* functions in the chain. And apart from being a maintenance horror,
it might be just impossible because the functions on the road are
already closed or just owned by someone else.
The tempting "solution" is to shut up the exception to meet the
specification which was already cast in stone. Just grep any bigger Java
project for things like:
catch (Throwable e) {} // <- empty block here!
to see it at work.
Java guys can at least try to fight this problem with inheritance. The
exception need not be exactly of the specified type, but might be
something derived from what was specified. So, the other temptation is
to specify the exception type that is rather general (higher in the
inheritance hierarchy) to ease the accommodation of new exception types.
But the more general is the specification, the less useful it is with
regard to enforcing anything. In the extreme, it does not enforce
anything at all.
(Note that Ada would not have this possibility, or it would need to
allow for exception hierarchies.)
Second, the problem is that the error reporting and handling might be
stated as a contract not between immediate caller and callee, but
between some entities that operate across some other entity. A C++
example could be:
void myFun()
{
vector<MyType> array;
// ...
sort(array.begin(), array.end(), myComparator());
// ...
}
Above, the call chain is myFun->sort->myComparator, but the error
handling and reporting is the business which is agreed between myFun and
myComparator only. The sort algorithm was written long before myFun and
myComparator and it (sort) does not care what it sorts and for whom.
This means that sort should be completely transparent to the agreement
that myFun and myComparator might have with regard to error reporting
and handling.
The solution might be to allow the compiler to synthesise the exception
specs for sort automatically. But then, the specs would be just useless,
because it would not enforce anything.
The real problem is that languages based on simple subroutine calls are
not appropriate for expressing these kind of relationships. Something
fundamentally different would be needed to ensure that myFun handles
exceptions from myComparator without involving sort in this process, but
I don't see what that thing would look like.
Anyway. The whole purpose of exceptions is to provide a kind of
"out-of-band" channel which is *decoupled* from the main chain of
subroutine calls. This "out-of-band" property is something that you
either like (and/or accept) or not in the given project. Messing around
with things like exception specifications is just swimming upstream -
you cannot provide coupling into something that was *intended* to be
"out-of-band" in the first place.
Java guys failed with this exercise. C++ community dropped the idea
altogether before failing (C++ never had compile-time enforcements of
exceptions specs and today nobody's using them anyway). I don't see how
Ada would do something like this without incurring effects described
above or without fundamentally changing something in the way subroutines
are used. But I'm looking forward to see your opinions on this (and
maybe learn something about Ada culture? :) ).
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
next prev parent reply other threads:[~2006-03-22 16:42 UTC|newest]
Thread overview: 91+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-03-13 19:58 private types ada_student
2006-03-13 20:27 ` Mark Lorenzen
2006-03-13 21:05 ` Pascal Obry
2006-03-13 21:07 ` ada_student
2006-03-13 21:45 ` Simon Wright
2006-03-14 4:51 ` Jeffrey R. Carter
2006-03-14 7:44 ` Brian May
2006-03-14 8:25 ` Ludovic Brenta
2006-03-14 8:47 ` Alex R. Mosteo
2006-03-17 4:33 ` Justin Gombos
2006-03-17 5:17 ` Brian May
2006-03-17 22:50 ` Justin Gombos
2006-03-18 1:17 ` Randy Brukardt
2006-03-18 2:17 ` Justin Gombos
2006-03-21 0:08 ` Randy Brukardt
2006-03-18 8:39 ` Uninitialized variables (was: Re: private types) Dirk Craeynest
2006-03-18 14:06 ` Gautier
2006-03-18 14:36 ` Uninitialized variables Jeffrey Creem
2006-03-21 0:22 ` Uninitialized variables (was: Re: private types) Randy Brukardt
2006-03-21 0:38 ` Randy Brukardt
2006-03-18 12:06 ` private types Martin Dowie
2006-03-18 12:47 ` Robert A Duff
2006-03-17 7:40 ` Maciej Sobczak
2006-03-17 16:41 ` Frank J. Lhota
2006-03-17 23:36 ` Justin Gombos
2006-03-18 1:32 ` Randy Brukardt
2006-03-18 3:21 ` Handling invalid objects Justin Gombos
2006-03-18 7:35 ` Jeffrey R. Carter
2006-03-18 16:10 ` Justin Gombos
2006-03-19 11:00 ` Simon Wright
2006-03-20 23:57 ` Randy Brukardt
2006-03-22 2:06 ` Justin Gombos
2006-03-22 5:23 ` tmoran
2006-03-22 8:48 ` Dmitry A. Kazakov
2006-03-22 9:24 ` Maciej Sobczak
2006-03-22 11:05 ` Dmitry A. Kazakov
2006-03-22 16:42 ` Maciej Sobczak [this message]
2006-03-22 18:06 ` Stefan Lucks
2006-03-23 13:20 ` Dmitry A. Kazakov
2006-03-18 8:57 ` Jacob Sparre Andersen
2006-03-19 19:07 ` Dr. Adrian Wrigley
2006-03-20 15:25 ` Robert A Duff
2006-03-19 22:06 ` Brian May
2006-03-20 21:17 ` Jeffrey R. Carter
2006-03-20 23:44 ` Randy Brukardt
2006-03-22 1:27 ` Justin Gombos
2006-03-18 9:20 ` private types Dmitry A. Kazakov
2006-03-17 13:18 ` Robert A Duff
2006-03-17 23:44 ` Justin Gombos
2006-03-18 9:24 ` Dmitry A. Kazakov
2006-03-18 12:56 ` Robert A Duff
2006-03-18 15:06 ` Justin Gombos
2006-03-19 9:35 ` Martin Krischik
2006-03-19 14:52 ` Peter C. Chapin
2006-03-19 15:08 ` Björn Persson
2006-03-19 16:34 ` Martin Krischik
2006-03-20 9:57 ` Maciej Sobczak
2006-03-20 10:58 ` Peter C. Chapin
2006-03-20 11:19 ` Peter C. Chapin
2006-03-20 13:06 ` Maciej Sobczak
2006-03-20 15:19 ` Robert A Duff
2006-03-20 16:47 ` James Dennett
2006-03-20 19:12 ` Martin Krischik
2006-03-21 7:27 ` Maciej Sobczak
2006-03-20 19:32 ` Martin Krischik
2006-03-21 7:41 ` Maciej Sobczak
2006-03-20 20:29 ` Simon Wright
2006-03-19 17:43 ` Larry Kilgallen
2006-03-19 22:11 ` Peter C. Chapin
2006-03-19 18:15 ` Robert A Duff
2006-03-19 19:20 ` Martin Krischik
2006-03-19 20:43 ` Dr. Adrian Wrigley
2006-03-20 15:01 ` Robert A Duff
2006-03-27 4:07 ` Dave Thompson
2006-03-20 9:40 ` Maciej Sobczak
2006-03-20 15:09 ` Robert A Duff
2006-03-21 8:07 ` Maciej Sobczak
2006-03-26 18:53 ` Robert A Duff
2006-03-19 19:27 ` Jeffrey R. Carter
2006-03-25 21:40 ` Robert A Duff
2006-03-26 0:10 ` Justin Gombos
2006-03-26 1:00 ` Robert A Duff
2006-03-26 6:37 ` Jeffrey R. Carter
2006-03-26 15:43 ` Justin Gombos
2006-03-26 16:32 ` Robert A Duff
2006-03-26 16:51 ` Robert A Duff
2006-03-26 19:41 ` Jeffrey R. Carter
2006-03-26 3:15 ` Frank J. Lhota
2006-03-26 18:28 ` Robert A Duff
2006-03-26 19:43 ` Jeffrey R. Carter
2006-03-26 19:59 ` Simon Wright
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox