Here's some sample code:
#include <exception>
class SysException { };
void f( ) throw (SysException)
{
throw std::exception();
}
int main()
{
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Calling f() should result in std::unexpected being called ...
[http://www.devx.com/tips/Tip/15140]
The Standard Library defines a function called std::unexpected() which
is invoked when a function throws an exception not listed in its
exception specification. std::unexpected invokes a user-defined function
that was registered by std::set_unexpected. If the user hasn't
registered such a function, unexpected() will invoke std::terminate(),
which in turn calls abort() and terminates the program. (...)
Exception specifications are apparently evil:
http://www.gotw.ca/publications/mill22.htm
br,
Martin
/Peter
--
Note that robinton.demon.co.uk addresses are no longer valid.
I may be wrong, but I believe that this code should fail to compile.
The exception specification for f() indicates that it will throw
objects only of type SysException. class SysException is not derived
from std::exception so throwing an object of class std::exception
would violate the exception specification.
Regards,
Jon Trauntvein
If you 'try' f(), then you can't 'catch' the std::exception.
I think such constructs should be warned by the compilers but my
gcc is happy with this stuff.
Maybe,
void f() throw (SysException, std::exception)
{ throw std::exception(); }
is what you needed.
No. On the contrary, a C++ compiler is not allowed to report an error
with f()'s definition above: According to the C++ Standard:
"An implementation shall not reject an expression merely because when
executed it throws or might throw an exception that the containing
function does not allow." [§15.4/10]
The fact that exception specifications are enforced only at runtime in
C++ (instead of at compile-time when such checking would be useful) -
makes exception specifications too risky for most real-world C++
applications to adopt.
Greg
No, the compiler should compile it because there is no error until f()
is called, and even then there is a well-defined action to be carried
out (call unexpected()) and so the code is still not erroneous.
--
Note that robinton.demon.co.uk addresses are no longer valid.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]