Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

void Foo::Bar()
{

    try
    {
       execute();
    }
    catch(const ns::Bad_Exception& e)
    {
       std::cout << "ns::Bad_Exception" << std::endl;
    }
}



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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by ZQJ
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).


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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this