Thomas J. Gritzan wrote:
as********@gmail.com schrieb:
Hi, just wondering if anybody can answer the following question as it
doesn't seem to be anywhere in the C++ spec.
The compiler can.
What will print from the following statement? (note the order of the
catch statements):
void func(void)
Abomination! (Cf.
<http://groups.google.com/group/comp.lang.c++/msg/895f1f98c4488dda>.)
{
try
{
const foo f1;
throw f1;
}
Since classes are thrown by value (copied), the const qualifier doesn't
affect the thrown object. So the body of catch(foo&) will run.
g++ also gives this warning:
$ g++ throw.cpp
throw.cpp: In function 'int main()':
throw.cpp:20: warning: exception of type 'foo' will be caught
throw.cpp:16: warning: by earlier handler for 'foo'
And the output:
$ ./a.out
foo &
I'd add to this that, when you can (which is almost always), you should
catch a *const* reference (see
<http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.7and
<http://www.parashift.com/c++-faq-lite/const-correctness.html>).
Cheers! --M