Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to catch c++ exception using catch (...)

I have a third-party library that is sometimes throwing an exception. So I decided to wrap my code in a try/catch(...) so that I could log information about the exception occurring (no specific details, just that it happened.)

But for some reason, the code still crashes. On client computers, it crashes hard and the code to log the exception in the catch(...) never gets executed. If I run this on my debug / development machine I get the popup asking me if I want to debug. When I do this, it reports 0xC0000005: Access violation reading location XXX.

The odd thing is that with an older version of the third-party library, the exact same code DOES catch the exception, and the code to log the exception DOES execute. (I verified this within VS watching the same conditions occur.)

Here's the pseudo-code that is executing:

pObject = pSystem->Get_pObject()
pSystem->DoSomethingThatMightDestroy_pObject();
try
{
    /*   Call to third party function that is throwing exception */
    pObject->SetValue(0);
}
catch (...)
{
    __DEBUG_LOG_POSITION__;  // A macro to log the current file line
    //  This code used to run in the older version of third-party library
    //  but the newer version just crashes before running the catch(...)
}

So I have two questions:

  1. Is there some change in the way the third party might have compiled the library so that my code wouldn't be able to catch the exception? (Yes, there is a chance I can get the third party to make whatever fixes are necessary and recompile for me, if I know what to tell them.)

  2. Assuming I can't get the third party to fix it, what can I do to catch these exceptions? I'm thinking along the lines of... is there some way for me to determine whether pObject was deallocated?

like image 858
Michael Bray Avatar asked Sep 03 '09 14:09

Michael Bray


People also ask

Can you catch exceptions in C?

C itself doesn't support exceptions but you can simulate them to a degree with setjmp and longjmp calls.

Which exception Cannot be caught in catch block?

Any exception that is thrown by the jitter before your code can start running cannot be caught or reported.

Does catch catch all exceptions C++?

C++ enables you to throw exceptions of any type, although in general it is recommended to throw types that are derived from std::exception. A C++ exception can be caught by a catch handler that specifies the same type as the thrown exception, or by a handler that can catch any type of exception.

How do you catch an exception in Objective C?

Exception handling is made available in Objective-C with foundation class NSException. @try − This block tries to execute a set of statements. @catch − This block tries to catch the exception in try block. @finally − This block contains set of statements that always execute.


2 Answers

AFAIK access violation don't throw exception... at least not standard ones!

Maybe catching windows-specific "native" exception would help : https://web.archive.org/web/20081022160935/http://www.gamedev.net/reference/articles/article2488.asp

like image 184
Klaim Avatar answered Sep 21 '22 13:09

Klaim


An access violation is not a C++ exception. It's a windows Structured Exception. You'll have to use _set_se_translator() if you want to catch them in catch(...).

You should probably google for all reasons catch(...) is evil and make sure you really want to do this.

like image 20
Steve Fallows Avatar answered Sep 21 '22 13:09

Steve Fallows