Article View: comp.std.c++
Article #741Re: Exception Specifications
From: Fergus James HENDERSON
Date: Sat, 03 Oct 1992 04:24
Date: Sat, 03 Oct 1992 04:24
80 lines
3506 bytes
3506 bytes
sakkinen@jyu.fi (Markku Sakkinen) writes:
>fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
>> ...
>>Actually, in C++ I would write
>> struct excp1 : Exception {};
>> ^^^^^^^^^^^
>
>What would that buy you? See below.
>
>>>Inheritance between exceptions may be treacherous, since 'throw'
>>>does not work "virtually". E.g., with Fergus's example:
>>> ...
>> ...
>>You can make throw behave virtually by throwing a pointer to the exception
>>instead of the exception itself:
>
>(Or a reference.) Yes, I must admit that this problem is not very severe.
You can't throw a reference.
Exception value;
Exception& reference = value;
throw reference;
This will throw the value refered to by the reference, not the reference
itself. [Well, this is what I infer from reading the RM]
>> throw new SomeException(...)
>>although you need to take care to ensure that the storage is correctly
>>deallocated by the handler, to avoid memory leaks.
>
>The example is not the best possible one, since here the exact type
>of the referenced object is in fact statically known.
>The handler has no way to know when it has to delete the exception object,
>short of there being a quasi-Boolean field 'delete_me' in Exception.
>Thus:
>
>struct Exception { int delete_me; };
>throw new Exception (1);
This throws an (Exception *)
>Exception ex (0);
>throw ex;
This just throws an Exception.
So you can distinguish between them by
try ...
catch(Exception *e_ptr) {
...
delete e_ptr;
}
catch(Exception& e) {
...
}
>And now to a perhaps better reason why using inheritance with exceptions
>(as suggested by you above) may be treacherous.
>Whenever there exists a handler for _any_ ancestor (base class) of the
>class of the exception object, that handler will be happily executed.
>Thus, if an instance of a class derived from Exception is thrown,
>there is no guarantee that any more specific handler than that
>of Exception will be invoked.
>This is essentially different from virtual functions, with which it is
>guaranteed that dynamic binding will find the function appropriate
>for the true class of the 'this' object.
Actually, this is just a reason to always catch exceptions by reference.
Then the handler can call virtual functions in the exception object.
Raising an exception is then equivalent to passing an object to a function
that takes a reference parameter. Of course, in both cases you can't
*guarantee* that the exception handler / receiving function will actually
call any virtual functions, but you know that if it does then the behaviour
will be appropriately polymorphic.
--
Fergus Henderson fjh@munta.cs.mu.OZ.AU
This .signature virus is a self-referential statement that is true - but
you will only be able to consistently believe it if you copy it to your own
.signature file!
Technical Details
Message-ID:
<9227714.13695@mulga.cs.mu.OZ.AU>Path:
arc1-ca.pugleaf.net!sparky!uunet!europa.asd.contel.com!darwin.sura.net!spool.mu.edu!uwm.edu!rpi!batcomputer!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjhReferences:
<5035@holden.lulea.trab.se> <1992Oct1.151052.7595@jyu.fi> <9227620.2176@mulga.cs.mu.OZ.AU> <1992Oct2.125244.14614@jyu.fi>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++ Path: arc1-ca.pugleaf.net!sparky!uunet!europa.asd.contel.com!darwin.sura.net!spool.mu.edu!uwm.edu!rpi!batcomputer!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) Subject: Re: Exception Specifications Message-ID: <9227714.13695@mulga.cs.mu.OZ.AU> Sender: news@cs.mu.OZ.AU Organization: Computer Science, University of Melbourne, Australia References: <5035@holden.lulea.trab.se> <1992Oct1.151052.7595@jyu.fi> <9227620.2176@mulga.cs.mu.OZ.AU> <1992Oct2.125244.14614@jyu.fi> Date: Sat, 3 Oct 1992 04:24:22 GMT Lines: 80