Public Group

# [C++] Caught exception still causes app to terminate....

This topic is 3878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

void Foo::Bar()
{

try
{
execute();
}
{
}
}


I am calling a function which is capable of throwing a number of exceptions, but it is only necessary that I anticipate one of them. So naturally I wrapped the code inside a try/catch. However, even after I catch the exception my application is still prematurely terminated. Am I missing something?

##### Share on other sites
That code looks fine. Do you get an meaningful error message when the program terminates (this would be compiler dependent)? Otherwise, how are you sure it's an ns::Bad_Exception that is really thrown?

If it's completely mysterious, the only thing that I can think of is that if an exception is thrown while another exception is in the process of being thrown, then your program will terminate (this is why you don't throw exceptions from destructors).

##### Share on other sites
Quote:
 Original post by ZQJThat code looks fine. Do you get an meaningful error message when the program terminates (this would be compiler dependent)? Otherwise, how are you sure it's an ns::Bad_Exception that is really thrown?If it's completely mysterious, the only thing that I can think of is that if an exception is thrown while another exception is in the process of being thrown, then your program will terminate (this is why you don't throw exceptions from destructors).

Wow, thanks you were right (sort of). The rest of the problem was to do with my own stupidity.

A stack object was being deleted during stack unwinding and threw an exception in its deconstructor. Since I am so clever I had the following code in the deconstructor:

Foo::~Foo(){			try	{		deleteResources();	}	catch(const BadException& e)	{		DEBUG_STRING_V(e.what());		exit(EXIT_FAILURE); // !!!! WTF was i thinking?		}	catch(...)	{		DEBUG_STRING_V("Exception thrown in Foo::~Foo()");		exit(EXIT_FAILURE); // !!!! WTF was i thinking?		}}

So in short I was terminating the application myself. And thoes 'useful' debug macros were turned off ... I hate those things.

Is my mechanism for preventing deconstructors from throwing sound (everything except for my call to exit() ... I don't know what I was smoking)?

##### Share on other sites
Despite the fact that C++ classes have constructors and that the prefix 'de' generally means 'to undo', the correct term is 'destructor' not 'deconstructor'. Just a little nitpicking :)

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631708
• Total Posts
3001840
×