473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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;
}
}

Sep 5 '06 #1
2 2015
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)
{
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
Sep 5 '06 #2
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

Sep 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Der Andere | last post by:
What exactly is the sense of using a "const" modifier before a function declaration? Actually, you can use it in three places. For instance, take a look at the following function declaration (from...
11
by: Markus.Elfring | last post by:
A couple of software provides const-incorrect programming interfaces. I guess that it is possible to develop const-correct APIs/SDKs from the beginning if a few basic design rules and patterns...
1
by: Morgan Cheng | last post by:
Hi, This isssue consfuse me. According to <Thinking in C++>, temporay variable is *const*. like class X{}; X f(){return X();} void g1(X&){} void g2(const X&){}
10
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
11
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
5
by: Pedro Sousa | last post by:
Hi, I'm trying to create an template class that represent points in all possible dimensions, what I've made until now is #ifndef _POINT_HPP_ #define _POINT_HPP_ #include <vector>
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
15
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.