Class hierarchy of exceptions (Ada, C++)

From: Ludovic Brenta (ludovic.brenta_at_insalien.org)
Date: 03/26/05


Date: Sat, 26 Mar 2005 11:19:18 +0100

Robert A Duff writes:
> Well, actually, during the Ada 9X design I tried to push for a
> class-hierarchy of exceptions. I don't like every detail of the way
> C++ does it, but at least in *this* regard, it's better than Ada.
>
> Jerry Coffin is wrong that Ada does not allow attaching information
> to exception, by the way. Ada allows attaching Strings, which is
> admittedly a kludge. Using the class-hierarchy, as Jerry advocates,
> would be cleaner, and type safe.

I kind of like Ada exceptions as they are now. Because one cannot
carry much information in them (apart from the kluge you mentioned),
one tends not to rely on them for the normal flow of operations. I
have seen Java programs that would rely on exceptions for all kinds of
things, leading to spaghetti code. In C++ I find it a bit odd that I
can throw and catch an entire hash table as an exception if I want to.

The C++ way of catching all exceptions of a class and its derived
classes can lead to confusion. One can have multiple exception
handlers for the same exception and it may not be immediately obvious
to the reader which one is called. I see this as a maintenance
problem.

Another concern of mine with exception classes is that they'd have to
be allocated on the heap. There are situations where there is no heap
to allocate from, or where dynamic allocation is forbidden. I think
it necessary to provide a simple exception mechanism that does not
require any dynamic allocation, much less dynamic dispatching.

Perhaps a good middle-ground would be an addition to Ada along the
lines of:

package Ada.Exceptions.Extra is
   type Extra_Information is abstract tagged null record;

   procedure Raise_Exception (E : in Exception_Id;
                              Information : in Extra_Information'Class);

   function Exception_Information (X : Exception_Occurrence)
      return Extra_Information'Class;
end Ada.Exceptions.Extra;

Programs that want to carry rich information with exceptions would be
allowed to do so, while "pragma Restrictions (No_Dispatch)" or
"No_Dynamic_Allocation" would guarantee, to those interested, that no
such thing happens.

I am aware that all this would require the compiler to provide two
exception mechanisms and two kinds of Exception_Occurrences, so I may
have opened a can of worms here. Just thinking out loud :)

-- 
Ludovic Brenta.


Relevant Pages

  • Re: [announcement] SYSAPI and SYSSVC for Windows
    ... What do you need Windows NT for in this application? ... You can use SEH (Structured Exception Handling) to prevent a process from ... > Ada did solve this, ... If you implement your own message queues on Windows, use an Event object ...
    (comp.lang.ada)
  • Re: Error-names.
    ... an exception you never thought about when designing the component. ... >> If you are writing a bridge that is controlled by C executable accessing an ada ... All exceptions are passed to the calling ... The advantage of this programming lifestyle which I learned from OLE(windows ...
    (comp.lang.ada)
  • Re: Class hierarchy of exceptions (Ada, C++)
    ... >> Jerry Coffin is wrong that Ada does not allow attaching information ... > can throw and catch an entire hash table as an exception if I want to. ... > require any dynamic allocation, ...
    (comp.lang.ada)
  • Re: Class hierarchy of exceptions (Ada, C++)
    ... >> Jerry Coffin is wrong that Ada does not allow attaching information ... > can throw and catch an entire hash table as an exception if I want to. ... > require any dynamic allocation, ...
    (comp.lang.cpp)
  • Re: In-Out Parameters for functions
    ... > the Ada mindset) was that the reason for l-d checks is to prevent the ... sense given the way today's CPUs are built. ... to avoid erroneousness in some contexts. ... -- object declarations here should never raise an exception. ...
    (comp.lang.ada)