Sign in to follow this  

Unhandled exceptions in C++

This topic is 4379 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
In your example catch(...) will catch exceptions that were thrown using 'throw'.

To catch a division by zero you may want to use SEH - structured exception handling. Search for EXCEPTION_INT_DIVIDE_BY_ZERO.

http://www.gamedev.net/reference/programming/features/sehbasics/

http://www.microsoft.com/msj/0197/exception/exception.aspx

http://www.iseran.com/Win32/FAQ/except.html

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

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