Hi,
Below is a simple code about exception.
#include <iostream>
using namespace std;
struct E {
const char* message;
E(const char* arg) : message(arg) { }
};
void my_terminate() {
cout << "Call to my_terminate" << endl;
};
struct A {
A() { cout << "In constructor of A" << endl; }
~A() {
cout << "In destructor of A" << endl;
throw E("Exception thrown in ~A()");
}
};
struct B {
B() { cout << "In constructor of B" << endl; }
~B() { cout << "In destructor of B" << endl; }
};
int main() {
set_terminate(my_terminate);
try {
cout << "In try block" << endl;
A a;
B b;
cout << "Leave try block" << endl;
throw("Exception thrown in try block of main()"); //LINE1
}
catch (const char* e) { //LINE2
cout << "Exception: " << e << endl; //LINE3
}
catch (...) {
cout << "Some exception caught in main()" << endl;
}
cout << "Resume execution of main()" << endl;
}
The output of the code is below:
In try block
In constructor of A
In constructor of B
Leave try block
In destructor of B
In destructor of A
Call to my_terminate
Abort
My question is when LINE1 throws an exception, the exception is caught
by LINE2, right?
Why LINE3 does not output anything?
The last line of the output is "Abort". Why is it from? Does the
function my_terminate() output the "Abort"?
Thanks
Jack 10 1631 ju******@gmail.com wrote: Below is a simple code about exception.
#include <iostream> using namespace std;
struct E { const char* message; E(const char* arg) : message(arg) { } };
void my_terminate() { cout << "Call to my_terminate" << endl; };
struct A { A() { cout << "In constructor of A" << endl; } ~A() { cout << "In destructor of A" << endl; throw E("Exception thrown in ~A()"); } };
struct B { B() { cout << "In constructor of B" << endl; } ~B() { cout << "In destructor of B" << endl; } };
int main() { set_terminate(my_terminate);
try { cout << "In try block" << endl; A a; B b; cout << "Leave try block" << endl; throw("Exception thrown in try block of main()"); //LINE1 } catch (const char* e) { //LINE2 cout << "Exception: " << e << endl; //LINE3 } catch (...) { cout << "Some exception caught in main()" << endl; }
cout << "Resume execution of main()" << endl; }
The output of the code is below: In try block In constructor of A In constructor of B Leave try block In destructor of B In destructor of A Call to my_terminate Abort
My question is when LINE1 throws an exception, the exception is caught by LINE2, right?
No. You're throwing an exception while stack is unwinding. That causes
the 'terminate' to be called.
Why LINE3 does not output anything?
Because the control never gets there.
The last line of the output is "Abort". Why is it from? Does the function my_terminate() output the "Abort"?
Not sure, but I think after the terminate handler 'abort' is called
regardless. In your implementation, 'abort' outputs "Abort", most
likely.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask ju******@gmail.com wrote: Hi, Below is a simple code about exception.
#include <iostream> using namespace std;
struct E { const char* message; E(const char* arg) : message(arg) { } };
void my_terminate() { cout << "Call to my_terminate" << endl; };
struct A { A() { cout << "In constructor of A" << endl; } ~A() { cout << "In destructor of A" << endl; throw E("Exception thrown in ~A()"); } };
struct B { B() { cout << "In constructor of B" << endl; } ~B() { cout << "In destructor of B" << endl; } };
int main() { set_terminate(my_terminate);
try { cout << "In try block" << endl; A a; B b; cout << "Leave try block" << endl; throw("Exception thrown in try block of main()"); //LINE1 } catch (const char* e) { //LINE2 cout << "Exception: " << e << endl; //LINE3 } catch (...) { cout << "Some exception caught in main()" << endl; }
cout << "Resume execution of main()" << endl; }
The output of the code is below: In try block In constructor of A In constructor of B Leave try block In destructor of B In destructor of A Call to my_terminate Abort
My question is when LINE1 throws an exception, the exception is caught by LINE2, right? Why LINE3 does not output anything?
The last line of the output is "Abort". Why is it from? Does the function my_terminate() output the "Abort"?
LINE2 would catch the char* exception you throw, except that before the
catch happens it must destroy all the automatic objects on the stack,
which includes A. Unfortunately, A's destructor itself throws an
exception, which is bad bad bad, and since you're already in the stack
unwinding process when that exception is thrown, you abort. That's how
the language is defined. See this FAQ, particularly the "Bang! You're
dead." part: http://www.parashift.com/c++-faq-lit....html#faq-17.3
The lesson here is: Don't throw exceptions in destructors.
Cheers! --M
Victor Bazarov wrote: The last line of the output is "Abort". Why is it from? Does the function my_terminate() output the "Abort"?
Not sure, but I think after the terminate handler 'abort' is called regardless. In your implementation, 'abort' outputs "Abort", most likely.
The terminate function is not supposed to return. Hence its name. <g> My
recollection is that the behavior of the program is undefined if it does
return, so the compiler's support library is being helpful by explicitly
calling abort.
--
Pete Becker
Roundhouse Consulting, Ltd.
mlimber wrote: The lesson here is: Don't throw exceptions in destructors.
The bromide here is: Don't throw exceptions in destructors. The behavior
of the program is well defined (aside from the invalid terminate
handler) and easily understood.
--
Pete Becker
Roundhouse Consulting, Ltd.
Thanks. http://www.parashift.com/c++-faq-lit....html#faq-17.3
I read the faq. Faq17.3 says that "For example, if someone says throw
Foo(), the stack will be unwound so all the stack frames between the
throw Foo() and the } catch (Foo e) { will get popped. This is called
stack unwinding."
But I can not understand it. When throw Foo(), the 'throw Foo()' is
located at the top of the stack, right? Where is 'catch (Foo e)' in the
stack? Are the } and { typos in the sentence above?
Whenever an exception is thrown, does stack unwinding happen?
Thanks.
Jack
Pete Becker wrote: mlimber wrote:
The lesson here is: Don't throw exceptions in destructors.
The bromide here is: Don't throw exceptions in destructors. The behavior of the program is well defined (aside from the invalid terminate handler) and easily understood.
Right, and I didn't say differently.
Cheers! --M ju******@gmail.com wrote: Thanks.
http://www.parashift.com/c++-faq-lit....html#faq-17.3 I read the faq. Faq17.3 says that "For example, if someone says throw Foo(), the stack will be unwound so all the stack frames between the throw Foo() and the } catch (Foo e) { will get popped. This is called stack unwinding."
But I can not understand it. When throw Foo(), the 'throw Foo()' is located at the top of the stack, right? Where is 'catch (Foo e)' in the stack?
Mr. Cline is describing what happens under the hood on many platforms.
There is no language requirement that a system stack even exist.
Perhaps, clearer would be this:
struct E1 {};
struct E2 {};
struct A { ~A() { throw E1(); } };
// ...
try
{
A a;
throw E2();
}
catch( const E1& e )
{
// ... never reached ...
}
catch( const E2& e )
{
// ... never reached ...
}
When MyException is thrown, a's destructor is called, but it also
throws an exception. Now, as per the language definition, you can only
handle one of these two exceptions at a time. So which one (E1 or E2?)
should the language pass on to your handlers? Since handling one means
the other is necessarily unhandled, the language just terminates the
program rather than try to decide for you.
Are the } and { typos in the sentence above?
He just means "catch( Foo e )" (or better, "catch( const Foo& e )",
which is in line with another FAQ: http://www.parashift.com/c++-faq-lit...html#faq-17.7).
Whenever an exception is thrown, does stack unwinding happen?
It's technically implementation dependent, but commonly, yes.
Cheers! --M
mlimber wrote: ju******@gmail.com wrote: Thanks.
http://www.parashift.com/c++-faq-lit....html#faq-17.3
I read the faq. Faq17.3 says that "For example, if someone says throw Foo(), the stack will be unwound so all the stack frames between the throw Foo() and the } catch (Foo e) { will get popped. This is called stack unwinding."
But I can not understand it. When throw Foo(), the 'throw Foo()' is located at the top of the stack, right? Where is 'catch (Foo e)' in the stack?
Mr. Cline is describing what happens under the hood on many platforms. There is no language requirement that a system stack even exist. Perhaps, clearer would be this:
struct E1 {}; struct E2 {}; struct A { ~A() { throw E1(); } };
// ... try { A a; throw E2(); } catch( const E1& e ) { // ... never reached ... } catch( const E2& e ) { // ... never reached ... }
When MyException is thrown, a's destructor is called, but it also throws an exception. Now, as per the language definition, you can only handle one of these two exceptions at a time. So which one (E1 or E2?) should the language pass on to your handlers? Since handling one means the other is necessarily unhandled, the language just terminates the program rather than try to decide for you.
Are the } and { typos in the sentence above?
He just means "catch( Foo e )" (or better, "catch( const Foo& e )", which is in line with another FAQ: http://www.parashift.com/c++-faq-lit...html#faq-17.7).
Whenever an exception is thrown, does stack unwinding happen?
It's technically implementation dependent, but commonly, yes.
Cheers! --M
Thanks a lot. I understand now. I make some changes to your code as
below:
struct E1 {};
struct E2 {};
struct A { ~A() { throw E1(); } }; //LINE0
// ...
try
{
A a;
//throw E2(); //LINE1
} //LINE2
catch( const E1& e )
{
// ... CAN BE reached ... //LINE3
}
catch( const E2& e )
{
// ... never reached ...
}
I comment LINE1. When LINE2 is reached, the object a will be destroyed,
since a is only valid within the try{} block. But this is NOT stack
unwinding. LINE0 will throw an exception. LINE3 can be reached. Is my
understanding correct?
Thanks.
Jack
junw2...@gmail.com wrote: mlimber wrote: ju******@gmail.com wrote: Thanks.
> http://www.parashift.com/c++-faq-lit....html#faq-17.3
I read the faq. Faq17.3 says that "For example, if someone says throw Foo(), the stack will be unwound so all the stack frames between the throw Foo() and the } catch (Foo e) { will get popped. This is called stack unwinding."
But I can not understand it. When throw Foo(), the 'throw Foo()' is located at the top of the stack, right? Where is 'catch (Foo e)' in the stack?
Mr. Cline is describing what happens under the hood on many platforms. There is no language requirement that a system stack even exist. Perhaps, clearer would be this:
struct E1 {}; struct E2 {}; struct A { ~A() { throw E1(); } };
// ... try { A a; throw E2(); } catch( const E1& e ) { // ... never reached ... } catch( const E2& e ) { // ... never reached ... }
When MyException is thrown, a's destructor is called, but it also throws an exception. Now, as per the language definition, you can only handle one of these two exceptions at a time. So which one (E1 or E2?) should the language pass on to your handlers? Since handling one means the other is necessarily unhandled, the language just terminates the program rather than try to decide for you.
Are the } and { typos in the sentence above?
He just means "catch( Foo e )" (or better, "catch( const Foo& e )", which is in line with another FAQ: http://www.parashift.com/c++-faq-lit...html#faq-17.7).
Whenever an exception is thrown, does stack unwinding happen?
It's technically implementation dependent, but commonly, yes.
Cheers! --M
Thanks a lot. I understand now. I make some changes to your code as below:
struct E1 {}; struct E2 {}; struct A { ~A() { throw E1(); } }; //LINE0
// ... try { A a; //throw E2(); //LINE1 } //LINE2 catch( const E1& e ) { // ... CAN BE reached ... //LINE3 } catch( const E2& e ) { // ... never reached ... }
I comment LINE1. When LINE2 is reached, the object a will be destroyed, since a is only valid within the try{} block. But this is NOT stack unwinding. LINE0 will throw an exception. LINE3 can be reached. Is my understanding correct?
Yes. LINE3 can be and *will* be reached, but remember that destructors
still should not throw exceptions since it can lead to subtle errors
(i.e., the program not performing up to the requirements) like the one
discussed above in this thread.
Cheers! --M
mlimber wrote: junw2...@gmail.com wrote: mlimber wrote: ju******@gmail.com wrote: > Thanks. > > > http://www.parashift.com/c++-faq-lit....html#faq-17.3 > > I read the faq. Faq17.3 says that "For example, if someone says throw > Foo(), the stack will be unwound so all the stack frames between the > throw Foo() and the } catch (Foo e) { will get popped. This is called > stack unwinding." > > But I can not understand it. When throw Foo(), the 'throw Foo()' is > located at the top of the stack, right? Where is 'catch (Foo e)' in the > stack?
Mr. Cline is describing what happens under the hood on many platforms. There is no language requirement that a system stack even exist. Perhaps, clearer would be this:
struct E1 {}; struct E2 {}; struct A { ~A() { throw E1(); } };
// ... try { A a; throw E2(); } catch( const E1& e ) { // ... never reached ... } catch( const E2& e ) { // ... never reached ... }
When MyException is thrown, a's destructor is called, but it also throws an exception. Now, as per the language definition, you can only handle one of these two exceptions at a time. So which one (E1 or E2?) should the language pass on to your handlers? Since handling one means the other is necessarily unhandled, the language just terminates the program rather than try to decide for you.
> Are the } and { typos in the sentence above?
He just means "catch( Foo e )" (or better, "catch( const Foo& e )", which is in line with another FAQ: http://www.parashift.com/c++-faq-lit...html#faq-17.7).
> Whenever an exception is thrown, does stack unwinding happen?
It's technically implementation dependent, but commonly, yes.
Cheers! --M
Thanks a lot. I understand now. I make some changes to your code as below:
struct E1 {}; struct E2 {}; struct A { ~A() { throw E1(); } }; //LINE0
// ... try { A a; //throw E2(); //LINE1 } //LINE2 catch( const E1& e ) { // ... CAN BE reached ... //LINE3 } catch( const E2& e ) { // ... never reached ... }
I comment LINE1. When LINE2 is reached, the object a will be destroyed, since a is only valid within the try{} block. But this is NOT stack unwinding. LINE0 will throw an exception. LINE3 can be reached. Is my understanding correct?
Yes. LINE3 can be and *will* be reached, but remember that destructors still should not throw exceptions since it can lead to subtle errors (i.e., the program not performing up to the requirements) like the one discussed above in this thread.
Cheers! --M
I got it.
Thank you very much.
Jack This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gonçalo Rodrigues |
last post by:
Hi,
For error processing I found convenient maintaining a dictionary where
the keys are exception *classes* and the values are callables. Of
course, for this to work, exception classes have to...
|
by: Fred Zwarts |
last post by:
Hello,
I am trying to debug some complex debug code.
In order to track the use of dynamically allocated memory,
I replaced the standard global new and delete operators.
(Not for changing the...
|
by: Bret Pehrson |
last post by:
Suppose the following:
// Unmanaged code
class UnmanagedException /* not visible outside of unmanaged code */
{
};
void DoSomething() /* visible (exported) to managed code */
{
throw new...
|
by: tony |
last post by:
Hello!!
As you know every user defined exception must be derived from class
Exception.
Now to my question if I write catch then every exception will be caught.
If I instead write...
|
by: JohnDeHope3 |
last post by:
First let me say that I understand that Asp.Net wraps my exception in an
HttpUnhandledException. I have found a lot of discussion about that on the
web, which was informative, but not helpful. Let...
|
by: Bry |
last post by:
I've created a class that offers an enhanced way of handling fatal
exceptions. The class allows the user to optionaly submit a http based
anonymous error report to myself, and also records details...
|
by: Zorro |
last post by:
The simplicity of stack unraveling of C++ is not without defective
consequences. The following article points to C++ examples showing the
defects. An engineer aware of defects can avoid...
|
by: Vijay |
last post by:
Hi All,
I am not able to figure out what exactly happening in below code. what
is control flow. Can anyone clear my confusion?
Code:
class A
{
public:
A(){cout<<"In Constructor\n";}
|
by: Bob Altman |
last post by:
Hi all,
We have a native class modeled after the System::Exception class, and all
exceptions that we throw derive from this class. For now this class is
quite simple: just Description and...
|
by: =?Utf-8?B?UmFq?= |
last post by:
How do I know which methods will throw exception when I am using FCL or other
third party .Net library?
I am developer of mostly native Windows applications and now .Net. After
working few...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |