exception raised in destructor

P

puzzlecracker

Allegedly, it is a well-known practice to avoid throwing an exception
from destructor. What are they - consequences and what a general
techniques when exception are possible (like fail to close db
connection or socket, etc)? BTW, how can you have two active exception
at the same time? What is the neteffect; program termination? Does
standard say anything in this regard?

Thanks
 
V

Victor Bazarov

puzzlecracker said:
Allegedly, it is a well-known practice to avoid throwing an exception
from destructor.

Why "allegedly"?
What are they - consequences and what a general
techniques when exception are possible (like fail to close db
connection or socket, etc)?

Program termination.
BTW, how can you have two active exception
at the same time?

I can't.
What is the neteffect; program termination? Does
standard say anything in this regard?

Yes, it does. See [except.terminate]/1, the third bullet point.

V
 
P

puzzlecracker



Is it not possible, even in theory, to have two or more active
exceptions simultaneously? I read somewhere - in effective c++ or
exceptional c++, don't remember exactly - where an author listed
circumstances where it was possible.


Thanks
 
V

Victor Bazarov

puzzlecracker said:
Is it not possible, even in theory, to have two or more active
exceptions simultaneously? I read somewhere - in effective c++ or
exceptional c++, don't remember exactly - where an author listed
circumstances where it was possible.

OK, it's possible. If any destructor during execution of its code
encounters any exception, it better catch it itself. At the time
between throwing such exception and catching it in the destructor
itself, there will be two exceptions "active" simultaneously. Here
is a contrived example:

void foo() throw(int) {
static int a = 42;
throw a++;
}

#include <iostream>

struct A {
~A() {
try {
foo();
}
catch (int i) {
std::cout << "caught " << i << " in ~A()\n";
}
}
};

int main() {
try {
A a;
foo();
}
catch (int i) {
std::cout << "caught " << i << " in main()\n";
}
}


V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top