Link to home
Start Free TrialLog in
Avatar of nv3prasad
nv3prasad

asked on

Hook For All exceptions in my application...

Is there any way that I can trap all my Application Exceptions before anyother handler could handle it.

Want a detailed explanation.
Avatar of jkr
jkr
Flag of Germany image

You can do that using 'SetUnhandledExceptionFilter()':


LONG WINAPI __XceptFilter   (   EXCEPTION_POINTERS* pExp)
{
    LONG    lnRC    =   EXCEPTION_EXECUTE_HANDLER;

    TRACE3  (   "__XceptFilter(): got exception, code == 0x%x @ 0x%x, flags == 0x%x\r\n",  
                            pExp->ExceptionRecord->ExceptionCode,
                ( DWORD)    pExp->ExceptionRecord->ExceptionAddress,
                            pExp->ExceptionRecord->ExceptionFlags
            );

    return( lnRC);
}

// in your code:

    SetUnhandledExceptionFilter (   __XceptFilter);

By calling this API, your function replaces 'UnhandledExceptionFilter()', which will usually signal the exception to the user with one of these 'nice' message boxes and terminate the program.
Avatar of mikeblas
mikeblas

But this hooks unhandled exceptions. Your function will be alled only when there is no handler. nv3prasad wanted a crack ant every exception before any other handler had a chance.

..B ekiM
Thanx for the reminder, Mike...

Should have remembered the good ol' "Think before you post an answer" rule ;-)
This might do the job, however.  

What exceptions are handled already that  SetUnhandledExceptionFilter won't catch?

(I don't know, I'm just curious about this.  I've used SetUnhandledExceptionFilter and it catches most errors that occur in a running app.)
You're right jhance, but

__try
{
 char* pc=NULL;

*pc='a';
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
}

would handle the exception before it reaches the filter...
As jkr implies, any exception which is actually handled won't reach your installed handler. nv3prasad wanted something to get all exceptions, handled or not.

..B ekiM
Avatar of nv3prasad

ASKER

Adjusted points to 100
mikeblas has got my point!

Can anyone provide me a way where I can give the first try to all possible exceptions?

Thank you Mike

Prasad
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> Only debuggers can do that.

Now we're getting somewhere!

Aside from writing a debugger to monitor your process, your only other alternative is to write a DLL that replaces and watches the RaiseException() API.

..B ekiM
I think Jkr has thrown a lot of light on this and also I thank Mike for his valuable suggestions.

I accept jkr's comment as the answer.