P
Paul Brettschneider
Hello everyone,
I wanted to start adding exception specifications (via throw(...)) to a
project of mine, since it looked like a good idea for the sake of
documentation, giving optimisation-hints to the compiler and getting better
compiler warnings.
Much to my surprise, the following test program did not give warnings, when
compiled with gcc 4.1.3:
class exception {};
class x {
int a;
public:
void m1() throw();
void m2() throw( exception );
};
void x::m1() throw()
{
a = 1;
throw exception();
}
void x::m2() throw(exception)
{
a = 2;
}
I would have expected that, since x::m1() clearly throws an exception and
x::m2() clearly does not throw an exception, the compiler would give
warnings. I figure that in some cases giving this kind of warning would
be difficult (e.g. pointers to functions and methods or calling of functions
in legacy libraries without exception specification). But in 99% of the
cases this would have been a very nice feature, if used with care.
So I assumed an quality-of-implementation issue. But after a little
googeling I found this document: http://www.gotw.ca/publications/mill22.htm
which claims that the exception specification is checked at *runtime*.
And the following test program confirms this:
// With the classes from above
#include <iostream>
int main(int argc, const char **argv)
{
x X;
try {
X.m1(); X.m2();
} catch(exception &e) {
std::cout << "caught exception" << std::endl;
}
return 0;
}
Indeed, the compiler goes out of its way to catch the exception thrown by m1
and call std::terminate(). This seems insane for the following reasons:
* Runtime-overhead.
* Very un-C++. (e.g. const, private, etc.. is checked at compile-time, not
at run-time).
* Not what one would expect.
So, to make a long story short, my questions are:
Is this really what the standard says?
Why does the C++-faq not mention this?
Is there a plan for sensible exception specification, that is one that is
checked at compile time and that enables the compiler to do code
optimisation?
Thanks.
PS: Sorry if this was already discussed at length before, but I was quite
shocked by this behaviour.
I wanted to start adding exception specifications (via throw(...)) to a
project of mine, since it looked like a good idea for the sake of
documentation, giving optimisation-hints to the compiler and getting better
compiler warnings.
Much to my surprise, the following test program did not give warnings, when
compiled with gcc 4.1.3:
class exception {};
class x {
int a;
public:
void m1() throw();
void m2() throw( exception );
};
void x::m1() throw()
{
a = 1;
throw exception();
}
void x::m2() throw(exception)
{
a = 2;
}
I would have expected that, since x::m1() clearly throws an exception and
x::m2() clearly does not throw an exception, the compiler would give
warnings. I figure that in some cases giving this kind of warning would
be difficult (e.g. pointers to functions and methods or calling of functions
in legacy libraries without exception specification). But in 99% of the
cases this would have been a very nice feature, if used with care.
So I assumed an quality-of-implementation issue. But after a little
googeling I found this document: http://www.gotw.ca/publications/mill22.htm
which claims that the exception specification is checked at *runtime*.
And the following test program confirms this:
// With the classes from above
#include <iostream>
int main(int argc, const char **argv)
{
x X;
try {
X.m1(); X.m2();
} catch(exception &e) {
std::cout << "caught exception" << std::endl;
}
return 0;
}
Indeed, the compiler goes out of its way to catch the exception thrown by m1
and call std::terminate(). This seems insane for the following reasons:
* Runtime-overhead.
* Very un-C++. (e.g. const, private, etc.. is checked at compile-time, not
at run-time).
* Not what one would expect.
So, to make a long story short, my questions are:
Is this really what the standard says?
Why does the C++-faq not mention this?
Is there a plan for sensible exception specification, that is one that is
checked at compile time and that enables the compiler to do code
optimisation?
Thanks.
PS: Sorry if this was already discussed at length before, but I was quite
shocked by this behaviour.