🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #737

Re: Exception Specifications

#737
From: Fergus James HENDERSON
Date: Fri, 02 Oct 1992 19:22
88 lines
3282 bytes
[Sorry if this reaches anyone twice.
 The previous version had an unfortunate typo.]

sakkinen@jyu.fi (Markku Sakkinen) writes:

>jbn@lulea.trab.se (Johan Bengtsson) writes:
>>fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
>>: sakkinen@jyu.fi (Markku Sakkinen) writes:
>>: 
>>: >I am afraid that distinguishing exceptions only by the type of the
>>: >'throw' expression is mostly overkill instead of improvement.
>>: > ...
>>: Is this so difficult?
>>: ...
>>: 	class MyException : Exception {};
>
>Here is only 1 exception, not 20.

I guess it is true that programmers just don't like verbosity.
OK, here is the succinctness-at-all-costs solution:

	#define e(x) class x : Exception {} ;
	e(e1)  e(e2)  e(e3)  e(e4)  e(e5)  e(e6)  e(e7)  e(e8)  e(e9)  e(e10)
	e(e11) e(e12) e(e13) e(e14) e(e15) e(e16) e(e17) e(e18) e(e19) e(e20)

Is that short enough, or should I remove all the unnecessary whitespace? ;-)

>In Ada, you would write:
>  excp1, excp2, excp2, ..., excp20: exception;
>In C++, you would write:
>  struct excp1 {};
>  struct excp2 {};
>   ...
>  struct excp20 {};
>and document somewhere that these classes are meant to function
>as "exception identifiers".

Actually, in C++ I would write
   struct excp1 : Exception {};
		^^^^^^^^^^^

>Inheritance between exceptions may be treacherous, since 'throw'
>does not work "virtually".  E.g., with Fergus's example:
>
>MyIntException mye (77);
>Exception *ep = mye;
This should be = &mye;
>throw *ep;
>
>Only Exception is thrown!

I think that this is unlikely to be a problem, since almost invariably
you know exactly what type of exception you wish to throw at the time
you are throwing it:

	if (some error condition) {
	    throw SomeException(...);
	}

It's a bit like constructors not being virtual: this is not a problem, since
when you are constructing an object, you know what type should be.

You can make throw behave virtually by throwing a pointer to the exception
instead of the exception itself:
	throw new SomeException(...)
although you need to take care to ensure that the storage is correctly
deallocated by the handler, to avoid memory leaks.

Note that "catch" *does* behave virtually.

	class Foo : Exception {
	    virtual void foo() { cout << "Foo::foo()" << endl; }
	};
	class Bar : Foo {
	    virtual void foo() { cout << "Bar::foo()" << endl; }

	main() {
	    try { throw Bar; }			// [ Fixed bug here :-) ]
	    catch (Foo& f) { f.foo(); }
	}

This will print Bar::foo(), not Foo::foo().

-- 
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: <9227705.8871@mulga.cs.mu.OZ.AU>
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
References: <9227515.19722@mulga.cs.mu.OZ.AU> <5035@holden.lulea.trab.se> <1992Oct1.151052.7595@jyu.fi>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++
Path: arc1-ca.pugleaf.net!sparky!uunet!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: <9227705.8871@mulga.cs.mu.OZ.AU>
Sender: news@cs.mu.OZ.AU
Organization: Computer Science, University of Melbourne, Australia
References: <9227515.19722@mulga.cs.mu.OZ.AU> <5035@holden.lulea.trab.se> <1992Oct1.151052.7595@jyu.fi>
Date: Fri, 2 Oct 1992 19:22:09 GMT
Lines: 89