Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when an exception is in flight?

In C++ (MSVC) how can I test whether an exception is currently "in flight". Ie, code which is being called as part of a class destructor may be getting invoked because an exception is unwinding the stack.. How can I detect this case as opposed to the normal case of a destructor being called due to a normal return?

like image 299
pauldoo Avatar asked Jul 27 '09 11:07

pauldoo


People also ask

What will happen when the handler is not found for exception?

3. What will happen when the exception is not caught in the program? Explanation: When exceptions are not caught in any program then program throws error.

How do you handle exceptions in C++?

C++ exception handling is built upon three keywords: try, catch, and throw. throw − A program throws an exception when a problem shows up. This is done using a throw keyword. catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem.

What is re throwing an exception means in C++?

If a catch block cannot handle the particular exception it has caught, you can rethrow the exception. The rethrow expression ( throw without assignment_expression) causes the originally thrown object to be rethrown.


1 Answers

Actually it's possible to do this, call uncaught_exception() in <exception> header. One reason you might want to do this is before throwing an exception in a destructor, which would lead to program termination if this destructor was called as part of stack unwinding. See http://msdn.microsoft.com/en-us/library/k1atwat8%28VS.71%29.aspx

like image 195
Diaa Sami Avatar answered Nov 05 '22 22:11

Diaa Sami