Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ETFBlade

Uncaught or Unexpected Exception Handling??

This topic is 5213 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

I had a question about how to handle uncaught or unexpected exceptions. I was reading through my C++ book, and it said that when an uncaught or unexpected exception occurs, normally it calls terminate and aborts the program. However, it also pointed out how you can overwrite the default terminate functions with your own functions to handle these exceptions so that the program doesn''t terminate unexpectedly. My question is then, what would you do in the catch block? Here''s some code for reference: //////////////////////////////////////////////////////////////////////////////// void uncaughtQuit() { std::cout << "Terminating due to uncaught exception!\n"; exit(5); } /////////////////////////////////////////////////////////////////////////////// void myUnexpected() { throw std::bad_exception(); } ///////////////////////////////////////////////////////////////////////////// set_terminate( uncaughtQuit ); // Override default terminate with custom terminate function set_unexpected( myUnexpected ); // Override how to handle unexpected exceptions with own function ////////////////////////////////////////////////////////////////////////////// //... //... //... // Now in the program somewhere.... try{ ... ... }; catch( std::bad_exception &e ) { ??????? // What do you do in here? // Anything you want? }; catch( exception &e ){ ??????? // And then what do you do in here? // I thought by overwriting the terminate function with one that // properly exits would call that function instead upon // encountering an uncaught exception? // So, why try to catch it?? }; //////////////////////////////////////////////////////////////////////////////// Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Essentially, your linker won''t start your program with main but with

int _main(int argc, char* argv[])
{
try
{
main(argc, argv);
}
catch(...)
{
terminate();
}
}


All you have to do is stop the exception from unwinding to this catch block and it won''t automatically call terminate/abort.

Share this post


Link to post
Share on other sites

  • 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!