Jump to content
  • Advertisement
Sign in to follow this  
mikeman

Unhandled exceptions in C++

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

This is a question I came up today. Can I possibly catch a runtime error, such as ,say, division by zero with try...catch block, and print the correct message? The MSDN says this should work: try { int i=0; i=10/0; } catch(...) { printf("Generic Exception"); } But, apart from the fact it doesn't work with VS2005, it doesn't really give me any information about the exception itself. I just know an exception have been thrown, and that's that. Also, another question: Is it possible to do this for all unhandled exceptions, even those that are outside try blocks? There is a default exception handler I can set with set_unexpected(), but when that function is reached you have no clue what kind of exception is it. If it's possible, I would like my program to print a message for all unhandled exceptions(e.g "Division by Zero") and give the user the choise to continue instead of terminating abnormally. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
You might try enabling Structured Exceptions in your project. They are Windows specific.

[Edited by - Rattrap on December 20, 2005 7:47:49 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
But, apart from the fact it doesn't work with VS2005, it doesn't really give me any information about the exception itself. I just know an exception have been thrown, and that's that.


Check out the Deep C++ series on MSDN. I've linked the article that will be of most interest to you.

EDIT: Link didn't want to work nicely with frames. You want to look at "Handling Exceptions, Part 17". It covers integrating structured exception handling with standard c++ exceptions.

Quote:
Original post by mikeman
Also, another question: Is it possible to do this for all unhandled exceptions, even those that are outside try blocks? There is a default exception handler I can set with set_unexpected(), but when that function is reached you have no clue what kind of exception is it.


I think this is also covered in the series.

Quote:
Original post by mikeman
If it's possible, I would like my program to print a message for all unhandled exceptions(e.g "Division by Zero") and give the user the choise to continue instead of terminating abnormally.


That's a bit more problematic. Whether it would even be a good idea to allow the user to continue is debatable, but that aside once the stack has been unwound there's no turning back. The only way of doing this would be to have a try/catch block around the inside of your message loop (or some equivalent), display something to the user and if the user chooses to continue just continue with the message loop and ignore the exception.

Share this post


Link to post
Share on other sites
One thing the documentation does not indicate is that if you choose to use _set_se_translator you need to use /EHa. This switch isn't available from the VS.NET 2003 GUI...I can't recall if it is in 2005 or not. The /EHa instructs the compiler to use the asynchronous exception handling model, which assumes that *every* statement may throw an exception. As a result, the compiler generates lifetime tracking code (that is, a list of destructors to run for objects in the current scope if an exception occurs) for *every* function. Under the synchronous model, it is able to optimize out some of this code if it can determine that no exceptions can be thrown from the current frame and all frames below it.

Thankfully, under VS.NET 2003, a C4535 warning is generated if you don't use /EHa when compiling.

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!