🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Exception Specifications

#731
From: Markku Sakkinen
Date: Thu, 01 Oct 1992 15:10
65 lines
2648 bytes
In article <5035@holden.lulea.trab.se> 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.

>: 
>: 	struct MyIntException : Exception {
>: 	    int value;
>: 	    MyIntException(int value_) : value(value_) {}
>: 	};

Likewise.

>
>Note that Markku said distinguishing exceptions only by the type.
>                                                ^^^^
>The alternative to class MyIntException with 20 distinct instances
>is 20 subclasses to MyIntException (which IMHO _is_ overkill).
> ...

Yes, that's what I had in mind.  Or actually that those exceptions
would not be related to each other at all, thus no inheritance relationship.
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".
(Admitted, if the exceptions are somehow related, one can declare
them as the constants of a new enumeration type, as suggested
in Stroustrup's book.  The situation is then almost as in Ada.)

Of course, one defect in Ada's exception mechanism is that no additional
information can be passed together with the exception itself,
and that has been corrected in C++.

Inheritance between exceptions may be treacherous, since 'throw'
does not work "virtually".  E.g., with Fergus's example:

MyIntException mye (77);
Exception *ep = mye;
throw *ep;

Only Exception is thrown!

----------------------------------------------------------------------
Markku Sakkinen (sakkinen@jytko.jyu.fi)
       SAKKINEN@FINJYU.bitnet (alternative network address)
Department of Computer Science and Information Systems
University of Jyvaskyla (a's with umlauts)
PL 35
SF-40351 Jyvaskyla (umlauts again)
Finland
----------------------------------------------------------------------
Technical Details
Message-ID: <1992Oct1.151052.7595@jyu.fi>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!news.funet.fi!network.jyu.fi!sakkinen
References: <9227515.19722@mulga.cs.mu.OZ.AU> <5035@holden.lulea.trab.se>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!news.funet.fi!network.jyu.fi!sakkinen
From: sakkinen@jyu.fi (Markku Sakkinen)
Subject: Re: Exception Specifications
Message-ID: <1992Oct1.151052.7595@jyu.fi>
Organization: University of Jyvaskyla, Finland
References: <9227515.19722@mulga.cs.mu.OZ.AU> <5035@holden.lulea.trab.se>
Date: Thu, 1 Oct 1992 15:10:52 GMT
Lines: 65