Jump to content
  • Advertisement
Sign in to follow this  
fpsgamer

[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.

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
Advertisement
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
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!