Sign in to follow this  

Exception handling question

This topic is 4074 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 was wondering if it was possible for protecting a whole program against bad exeptions doing this:
[source lang = "cpp"]

int WINAPI WinMain(...)
try {

   // ALL THE APPLICATION THINGS

} catch(..) {

    return -1;
}

return 0;


I mean, how good or how bad would that be? (Sorry if it's a noob question :) )

Share this post


Link to post
Share on other sites
The compiler typically does that for you already, except the code is more like


int WINAPI WinMainCRTStartup(void)
{
int ret = 0;
try {
ret = WinMain(...);
} catch(...) {
ret = -1;
}
return ret;
}


Where the try-catch in this psuedo code is termed the "top-level exception handler" [1].

Here are my notes on what happens when an exception is thrown.

Quote:

Raising an exception causes the exception dispatcher to go through the
following search for an exception handler:

1. The system first attempts to notify the process's debugger, if any.
2. If the process is not being debugged, or if the associated debugger does not
handle the exception, the system attempts to locate a frame-based exception
handler by searching the stack frames of the thread in which the exception occurred.
3. The system searches the current stack frame first, then proceeds backward
through preceding stack frames. If no frame-based handler can be found, or no
frame-based handler handles the exception, the system makes a second attempt
to notify the process's debugger.
4. If the process is not being debugged, or if the associated debugger does
not handle the exception, the system provides default handling based on the
exception type. For most exceptions, the default action is to call the
ExitProcess function.


And these articles might help as well.

Under the hood April 1997
Bugslayer August 1998
How a C++ compiler implements exception handling
Several classes for exception handling
Fault Protection: Using Windows Exception Filters
Creating Small Win32 Executables - Fast Builds

Share this post


Link to post
Share on other sites
As LessBread said, this already happens. Something that I find useful though is this...


int WINAPI WinMain(...)
{
#ifdef NDEBUG
try {
#endif

// ALL THE APPLICATION THINGS

#ifdef NDEBUG
} catch(const std::exception& e) {
// Display a useful error message, write an
// entry into a log file, or something else...
throw;
} catch(...) {
// Likewise, but we really don't know anything
// about the exception (eg. an access violation)
throw;
}
#endif
}



The reason I use the '#ifdef NDEBUG' is to prevent the exception from being caught in debug mode because presumably you're running inside the debugger and want execution to stop where the original is thrown, not where it gets re-thrown in your top-level handler.

Share this post


Link to post
Share on other sites
That really helped a LOT!! It resolved many doubts... Those articles from LessBreak also helped, thanks for taking the time.

Thanks!!!


(and thanks for the fast response also )

Share this post


Link to post
Share on other sites

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