Connecting Tech Pros Worldwide Help | Site Map

const exceptions

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 5th, 2006, 09:55 PM
aspestrand@gmail.com
Guest
 
Posts: n/a
Default const exceptions

Hi, just wondering if anybody can answer the following question as it
doesn't seem to be anywhere in the C++ spec.

What will print from the following statement? (note the order of the
catch statements):

void func(void)
{
try
{
const foo f1;
throw f1;
}
catch(foo &)
{
cout << "foo &" << endl;
}
catch(const foo &)
{
cout << "const foo &" << endl;
}
catch(...)
{
cout << "..." << endl;
}
}


  #2  
Old September 5th, 2006, 10:25 PM
Thomas J. Gritzan
Guest
 
Posts: n/a
Default Re: const exceptions

aspestrand@gmail.com schrieb:
Quote:
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.
Quote:
What will print from the following statement? (note the order of the
catch statements):
>
void func(void)
{
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 &

--
Thomas
http://www.netmeister.org/news/learn2quote.html
  #3  
Old September 6th, 2006, 03:45 PM
mlimber
Guest
 
Posts: n/a
Default Re: const exceptions

Thomas J. Gritzan wrote:
Quote:
aspestrand@gmail.com schrieb:
Quote:
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.
>
Quote:
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>.)
Quote:
Quote:
{
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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.