Re: Rethrow from other place than catch-block
mathiasn wrote:
Is it legal to rethrow an exception from a function called from the
catch block?
It must be legal.
In particular, ARM contains the following example:
void pass_through() { throw; }
void g()
{
PFV old = set_unexpected(&pass_through);
try { f(); }
catch (X) { }
catch (Y) { }
catch (...) { }
set_unexpected(old);
}
And my Russian "Interfaces and Messages" tutorial uses this technique to
implement common newCException() handler:
void f()
{
try { g(); }
catch (...) {
throw newCException(_FLINE_, "Problems in f()", toCException(_FLINE_));
}
}
sh_ptr<CException> toCException(const CException::FileLine& location)
{
#ifndef DERS_RETHROW_BUG // normal version
try { throw; }
catch (sh_ptr<CException> ce) {
return ce;
}
catch (const exception& e) {
return newSTDExternalCException(location, e.what(), typeid(e).name());
}
catch (...) {
return newUnknownExternalCException(location, "Unknown exception",
"unknown");
}
#else // version for broken compilers
if (CException::current.refs()>1 && CException::current.get())
return CException::current;
return newUnknownExternalCException(location, "Unknown exception",
"unknown");
#endif
}
You can download the source code http://ders.angen.net/cpp3/intmsg/code.zip
and run ceexample.cpp
This small testprogram compiles and runs as expected with Visual C++
2005. But in our bigger system we are expecting that code like this is
causing problems.
Yes, it does.
Unfortunately, I have to define DERS_RETHROW_BUG macro if the nested
rethrowing isn't supported by a particular compiler.
--
With all respect, Sergey. http://ders.angen.net/
mailto : ders at skeptik.net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]