views:

202

answers:

3

Hello

I am using Visual Studio 2005.
I am using SetUnhandledExceptionFilter to call my custom exception filter function when some unexpected error will happen. My custom filter function is called only if the unexpected error is Operating sytem error like Access Violation.

When the exception is C++ exception , raised with throw, my custom exception filter is not called, but still sometimes is called. Why is this ?

Thank you

A: 

Because when you throw something and it is catched by something, "SetUnhandledExceptionFilter" will not be used. So I guess some exceptions you throw are catched either by you or by some library (or something) you use.

int main(){
    try {
        //exception 1 thrown
    } catch (...){
       // exception 1 handling
    }

    // exception 2 thrown
}
// no catch for exception 2, use UnhandledExceptionFilter
anyone
C++ exceptions are not the same as structured exceptions.
Andy Johnson
Yes but I am not catching the c++ exception in my code for sure. If I dont catch the c++ exception my unhandled exception filter should be called, right ?
No. An unhandled C++ exception results in a call to the terminate function, as described here: http://msdn.microsoft.com/en-us/library/ac9f67ah%28VS.80%29.aspx. No structured exception is raised (unless you explicitly raise one in the terminate function by calling RaiseException() yourself!)
Andy Johnson
Then why sometimes, dont know to say when :( , my unhandled exception filter is called ? That is, it is called for some uncaught c++ exceptions, and for some uncaught c++ exceptions it is not.
Which exception code (e.g. EXCEPTION_ACCESS_VIOLATION) is being passed to your unhandled exception filter when it is called?
Andy Johnson
I really can't see which exception code gets the unexpected filter when it is called because of uncaught C++ exception. The debugger does not stop in the filter function, this is because the unhandled exception filter is not executed when the program is debugged. So I tried to write the excption code in a file and it is 3765269347670A9040. Does not make much sense, cause I can not map it to exisitng EXCEPTION_ flag.
Ok, I think I found out that someone else is overwriting my unhandled exception filter, and that's way the handler was not called. I just need to figure out who is doing this and how to stop it.
+2  A: 

SetUnhandledExceptionFilter() is called when a structured exception is thrown and there isn't a handler to catch the exception. Structured exceptions are not the same as C++ exceptions. Thats why SetUnhandledExceptionFilter() isn't being called: you are throwing a C++ exception, not a structured exception.

Structured exceptions are a language-independent exception handling mechanism provided by Windows itself. A good place to read about them is here. You thrown a structured exception using the RaiseException() API function, and you catch them (in C/C++) using the __try and __except keywords.

Andy Johnson
Does this mean that my filter will not be called if I am throwing c++ exception which is not caught ? I know for sure that it if will be not called if I catch the exception.
You asked: "Does this mean that my filter will not be called if I am throwing c++ exception which is not caught?". This is correct.The only way the filter can be called by throwing a c++ exception is if the act of throwing (or catching) the C++ exception subsequently causes a structured exception to be raised.The two exception handling systems are completely separate.
Andy Johnson
+2  A: 

You mixed up two different things:

  • Windows structured exceptions
  • C++ exceptions

For structured exceptions, you can specify a handler function with SetUnhandledExceptionFilter. A similar concept exists for C++ exceptions. Here you use the function set_unexpected or set_terminate. Your terminate handler should terminate the application whereas the unexpected handler could also throw (another) exception type. Thus you can catch "foreign" exceptions globally and map them to a exception type of your choice.

ur
Yes but , aren't c++ exceptions implemented using SEH internally ? If yes, my unexpected filter installed with SetUnhandledExceptionFilter should be executed. Note that SOMETIMES my unhandled exception filter is executed when c++ exception is not handled by catch is thrown. And sometimes is not. I can't figure it out the sometimes part.