473,748 Members | 4,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Throw from a destructor

C++ers:

I have this friend who thinks C++ cannot throw from a destructor.

I think throwing from a destructor is bad Karma, and should be designed
around, but that C++ cannot strictly prevent the actual event.

(One compiler-oriented reason is a compiler might not be able to detect
that the call-tree from a destructor leads to a throw.)

Put another way, suppose I have a big object, aBigSimCity, and it has a
..shutdown() method. It must call before ~BigSimCity() calls, and it might
throw. This implies my Wrapper class, containing aBigSimCity, should never
declare a destructor like this: ~Wrapper() { aBigSimCity.shu tdown(); }.

..shutdown() might throw, and I don't feel like wrapping it in
try{}catch(). ~Wrapper should not throw, hence I need a better system to
break things down.

So which of us is right?

--
Phlip
Jul 7 '06
17 3411
James Bannon <ja**********@n tlworld.comwrot e:
2. If, for some reason, your lucky enough not to have terminate
called, what do you do with the exception when you've caught it?
Re-throw if you can't recover? You're in terminate land. In any case,
how do you resuscitate or kill a partially dead object when you have
no way of knowing what state the object is in because that information
isn't conveyed to you by the exception class?

Sutter and others advise writing destructors as if they had an empty
throw specification. That's good enough for me! If you do specify it,
say ~T() throw() {...}, at least if an exception is leaked then
terminate will be called immediately.
Actually, it will call unexpected(), which I believe by default will
call terminate(), but this behavior can be overridden with
std::set_unexpe cted().
However, most people don't use
throw specifications at all primarily because they're expensive at
runtime and sometimes they don't buy you very much in the way of
safety
http://www.gotw.ca/gotw/082.htm
seems to agree.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 13 '06 #11
Marcus Kwok wrote:
James Bannon <ja**********@n tlworld.comwrot e:
>2. If, for some reason, your lucky enough not to have terminate
called, what do you do with the exception when you've caught it?
Re-throw if you can't recover? You're in terminate land. In any case,
how do you resuscitate or kill a partially dead object when you have
no way of knowing what state the object is in because that information
isn't conveyed to you by the exception class?

Sutter and others advise writing destructors as if they had an empty
throw specification. That's good enough for me! If you do specify it,
say ~T() throw() {...}, at least if an exception is leaked then
terminate will be called immediately.

Actually, it will call unexpected(), which I believe by default will
call terminate(), but this behavior can be overridden with
std::set_unexpe cted().
> However, most people don't use
throw specifications at all primarily because they're expensive at
runtime and sometimes they don't buy you very much in the way of
safety

http://www.gotw.ca/gotw/082.htm
seems to agree.
You're right of course, it does call unexpected() which, if not redefined, will call
terminate. But what can you do within std::set_unexpe cted() if a destructor
unexpectedly throws? By the time it gets to the unexpected handler you've no way of
knowing which destructor caused the problem, so you might as well terminate anyway
although it might be a bit more graceful that a core dump.

Jim.
Jul 13 '06 #12
James Bannon wrote:
For instance I still can't find the specific definition of the term POD. I
know what a POD is (roughly) but I can't find a specific definition in
the Standard other than a note to say that the acronym POD stands for
"Plain Old Data".
See 3.9/10 and 9/4
Jul 13 '06 #13
red floyd wrote:
James Bannon wrote:
>For instance I still can't find the specific definition of the term
POD. I know what a POD is (roughly) but I can't find a specific
definition in the Standard other than a note to say that the acronym
POD stands for "Plain Old Data".

See 3.9/10 and 9/4
Thanks red,
I have it! 9/4 is interesting as an English example of a self-referential definition. :)

Jim.
Jul 14 '06 #14
"James Bannon" <ja**********@n tlworld.comwrot e in message
news:ji******** ***********@new sfe4-win.ntli.net...
red floyd wrote:
>James Bannon wrote:
>>For instance I still can't find the specific definition of the term
POD. I know what a POD is (roughly) but I can't find a specific
definition in the Standard other than a note to say that the acronym POD
stands for "Plain Old Data".

See 3.9/10 and 9/4

Thanks red,
I have it! 9/4 is interesting as an English example of a self-referential
definition. :)
Really? I'd like to read it but don't have the standard. Is it something
like the definition of recursion?

Recursion: See recursion.
Jul 14 '06 #15
Noah Roberts wrote:
It is impossible to write exception safe code if any destructor can
throw. Destructors must therefore ALWAYS have the no-throw guarantee.
If this means having catch(...) then so be it...never allow exceptions
to escape a destructor...ne ver.
Thanks guys. Here's why I asked the question.

Some C++ test rigs support an explicit setUp() and tearDown() for their test
fixtures.

Some (despite being written by authors of books on C++) only provide the
constructor and destructor of the fixture.

I had to make a case for always providing tearDown(). For example, my
current project must call orb->shutdown() after each test case, and that
might throw. I need the throw reliably caught, without impossible
ramifications.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 14 '06 #16
Jim Langston wrote:
"James Bannon" <ja**********@n tlworld.comwrot e in message
news:ji******** ***********@new sfe4-win.ntli.net...
>red floyd wrote:
>>James Bannon wrote:
For instance I still can't find the specific definition of the term
POD. I know what a POD is (roughly) but I can't find a specific
definition in the Standard other than a note to say that the acronym POD
stands for "Plain Old Data".
See 3.9/10 and 9/4
Thanks red,
I have it! 9/4 is interesting as an English example of a self-referential
definition. :)

Really? I'd like to read it but don't have the standard. Is it something
like the definition of recursion?

Recursion: See recursion.

Something like that except it's of the form POD: see non-POD.

The wording I have is:

"...A POD-struct is an aggregate class that has no non-static data members of type
non-POD-struct, non-POD-union (or arrays of such types) or reference, and has no
user-defined copy assignment operator and no user-defined destructor...."

Hmmm.... clear enough? :)

Jim.
Jul 14 '06 #17
>James Bannon <ja**********@n tlworld.comwrot e:
>>Sutter and others advise writing destructors as if they had an empty
throw specification. That's good enough for me! If you do specify it,
say ~T() throw() {...}, at least if an exception is leaked then
terminate will be called immediately.
Marcus Kwok wrote:
>Actually, it will call unexpected(), which I believe by default will
call terminate(), but this behavior can be overridden with
std::set_unexp ected().
James Bannon <ja**********@n tlworld.comwrot e:
You're right of course, it does call unexpected() which, if not
redefined, will call terminate. But what can you do within
std::set_unexpe cted() if a destructor unexpectedly throws? By the time
it gets to the unexpected handler you've no way of knowing which
destructor caused the problem, so you might as well terminate anyway
although it might be a bit more graceful that a core dump.
Well, unexpected() seems to be pretty limited.

http://www.gotw.ca/gotw/082.htm basically says you can either 1)
translate the exception into something allowed by the exception
specification, or 2) call terminate(). But IIRC if a destructor throws
an exception in the process of stack unwinding, then terminate() gets
called immediately, the rationale being that if there are two active
exceptions, it is undetermined which one should get priority, so the
program just dies.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 14 '06 #18

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

Similar topics

4
1984
by: Eric Lilja | last post by:
Hello, in my program I have a function (pseudo code): void start_mysql_service() { obtain handle start mysql service using handle if start fails close handle and throw an exception containing error description
3
3501
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? Is this because there could be an exception thrown when the code creates a std::exception? I would assume that is not the case. However, if I want to create a new exception class, derived from std::exception (say MyException) then how can I...
1
1380
by: qazmlp | last post by:
I have a class like this: class base { public: virtual ~base() throw(); // Other members }; Now, if I write a class like this:
3
1713
by: Shark | last post by:
I read that in the C++ standard library destructors are not supposed to throw exceptions. Is this true of only the library, or is it true of C++ classes in general?
5
1836
by: Kenneth Porter | last post by:
I've read this article and have some followup questions. http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thr ead/9d5324ce02f4d89b/ I'm working on an embedded robotics application that cannot terminate. I often have the need to temporarily change some setting before performing an operation. If the operation fails, I still need to restore the original setting. It's possible for the restore operation to fail
8
1991
by: Grizlyk | last post by:
Hello I want to understand the exception habdling in C++ more, because i think no one can apply anything free (free - without remembering large unclear list of rules and exceptions from rules and exceptions from exception from rules) if he does not understand "why" the thing have done excactly as is. I know, some people do not agree with me at the point of "why".
5
478
by: siddhu | last post by:
What happens when deletion falis?Does operator delete function throw exception? I assume that nothrow is not available with delete. Or is it available?
5
1879
by: stevewilliams2004 | last post by:
I was wondering if someone could explain the output I am getting for the program below. What I expected from the main program output was "Cat" but instead I see "Mammal". The output is also included below. I got the same results with GCC 3.4.4 under cygwin as with MSDev studio 2003. Even stranger to me, if I change the catch statement to catch a Cat instead of a Mammal, the program crashes in the catch body, during the call to...
28
3807
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
0
8987
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9534
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.