Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catch(...) is not catching an exception, my program is still crashing

I'm having a problem with a tester that my application crashes in initialization. I added more logging and exception handling but it still crashes with the generic "this program has stopped working" message rather than triggering my error handling.

Given my main() looks like this and has catch(...) under what circumstances would this not be triggered?

try{
    simed::CArmApp app(0, cmd);
    for(bool done = false;!done;) 
    {
        done = !app.frame();
    }
} catch(const std::runtime_error &e){
    handleApplicationError(e.what());
    return -1;
} catch(...) {
    handleApplicationError("Unknown Error");
    return -999;
}

My code is calling into a library doing OpenGL rendering which is where I believe things are going wrong.

like image 578
Mr. Boy Avatar asked May 08 '14 13:05

Mr. Boy


People also ask

Does Try Catch prevent crash?

And yes, it prevents crashes; but leads to all kind of "undefined" behavior. You know, when you just catch an exception instead of properly dealing with it; you open a can of worms; because you might run into myriads of follow-on errors that you later don't understand.

What happens if a thrown exception is not caught?

What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console.

Why we should not catch exception?

Why is catch(Exception) almost always a bad idea ? Because exceptions if they are catched in the code, you need to handle them properly. And we cannot handle all types of exceptions. Some of the exceptions we cannot deal with it and they makes the application unstable like Thread abort exception.

Does catch exception catch everything?

If you catch some Exception types and don't do anything with the information, you have no chance of knowing what went wrong in those situations, but if you catch all Exception subclasses you have no chance of knowing what went wrong in a much large number of situations.


1 Answers

If a C++ catch(...) block is not catching errors maybe it is because of a Windows error.

On Windows there is a concept called Structured Exception Handling which is where the OS raises "exceptions" when bad things happen such as dereferencing a pointer that is invalid, dividing by zero etc. I say "exceptions" because these are not C++ exceptions; rather these are critical errors that Windows defines in a C-style fashion - this is because Win32 was written in C so C++ exceptions were not viable.

See also:

  • Difference between a C++ exception and Structured Exception
  • try-except Statement
  • Method of getting a stack trace from an EXCEPTION_POINTERS struct

Update based on comments

If you want both C++ exception handing and SEH perhaps you could try the following (untested) code:

__try
{
    try
    {
        // Your code here...
    }
    catch (std::exception& e)
    {
        // C++ exception handling
    }
}
__except(HandleStructuredException())
{
    // SEH handling 
}
like image 72
Peter Monks Avatar answered Oct 20 '22 19:10

Peter Monks