Article View: comp.std.c++
Article #736Re: Exception Specifications
From: Niklas Hallqvist
Date: Fri, 02 Oct 1992 13:33
Date: Fri, 02 Oct 1992 13:33
79 lines
2108 bytes
2108 bytes
sakkinen@jyu.fi (Markku Sakkinen) writes:
: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!
Of course, that's no different from an ordinary call. If you want
virtualness, you have to use pointers or references, as usual.
class Failure {
public:
virtual int retry() = 0;
};
template < class T >
class MemFailure {
int (*a_retry)(const T&);
T a_context;
protected:
virtual void get_more_memory () { free (some_extra_storage); }
public:
MemFailure (int (*the_retry)(const T&), T the_context)
: a_retry (the_retry), a_context (the_context) {}
int retry() { get_more_memory (); return a_retry(a_context); }
};
void foo()
{
try
{
bar ();
}
catch (Failure f) // Oops!
{
if (f.retry ()) // Pure method call :-(
throw;
}
try
{
bar ();
}
catch (Failure& f) // OK!
{
if (f.retry ())
throw;
}
}
int my_retry (int)
{
return 1;
}
void bar ()
{
static MemFailure<int> mf(my_retry, 0);
Failure& rf = mf;
Failure* pf = &mf;
switch (rand () & 1)
{
case 0:
throw rf; // OK!
break;
case 1:
throw *pf; // Oops, slicing :-(
break;
}
}
Niklas
--
Niklas Hallqvist Phone: +46-(0)31-40 75 00
Applitron Datasystem Fax: +46-(0)31-83 39 50
Molndalsvagen 95 Email: niklas@appli.se
S-412 63 GOTEBORG, Sweden mcsun!seunet!appli!niklas
Technical Details
Message-ID:
<2142@appli.se>Path:
arc1-ca.pugleaf.net!sparky!uunet!kithrup!stanford.edu!ames!haven.umd.edu!darwin.sura.net!Sirius.dfn.de!dct.zrz.tu-berlin.de!news.netmbx.de!Germany.EU.net!mcsun!sunic!seunet!appli!niklasReferences:
<9227515.19722@mulga.cs.mu.OZ.AU> <5035@holden.lulea.trab.se> <1992Oct1.151052.7595@jyu.fi>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!kithrup!stanford.edu!ames!haven.umd.edu!darwin.sura.net!Sirius.dfn.de!dct.zrz.tu-berlin.de!news.netmbx.de!Germany.EU.net!mcsun!sunic!seunet!appli!niklas From: niklas@appli.se (Niklas Hallqvist) Newsgroups: comp.std.c++ Subject: Re: Exception Specifications Message-ID: <2142@appli.se> Date: 2 Oct 92 13:33:43 GMT References: <9227515.19722@mulga.cs.mu.OZ.AU> <5035@holden.lulea.trab.se> <1992Oct1.151052.7595@jyu.fi> Organization: Applitron Datasystem AB, GOTHENBURG, SWEDEN Lines: 79