Solved

Hook For All exceptions in my application...

Posted on 2000-02-23
11
224 Views
Last Modified: 2013-11-20
Is there any way that I can trap all my Application Exceptions before anyother handler could handle it.

Want a detailed explanation.
0
Comment
Question by:nv3prasad
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 2550479
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.
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2552069
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 2552137
Thanx for the reminder, Mike...

Should have remembered the good ol' "Think before you post an answer" rule ;-)
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 32

Expert Comment

by:jhance
ID: 2552295
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.)
0
 
LVL 86

Expert Comment

by:jkr
ID: 2552395
You're right jhance, but

__try
{
 char* pc=NULL;

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

would handle the exception before it reaches the filter...
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2552899
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
0
 
LVL 1

Author Comment

by:nv3prasad
ID: 2553013
Adjusted points to 100
0
 
LVL 1

Author Comment

by:nv3prasad
ID: 2553014
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
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 2554329
>>Can anyone provide me a way where I
>>can give the first try to all
>>possible exceptions?

Only debuggers can do that. However, writing such a 'mini' debugger isn't too difficult:

    DWORD       dwStatus;

    HANDLE      hProcess    =   INVALID_HANDLE_VALUE;

    STARTUPINFO         si;
    PROCESS_INFORMATION pi;

    ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

    si.cb           =   sizeof  (   STARTUPINFO);
    si.dwFlags      =   STARTF_USESHOWWINDOW;
    si.wShowWindow  =   SW_SHOWDEFAULT;

    if  (   !CreateProcess  (   NULL,
                                pszModule,
                                NULL,
                                NULL,
                                FALSE,
                                DEBUG_ONLY_THIS_PROCESS | CREATE_NEW_CONSOLE,
                                GetEnvironmentStrings   (),
                                NULL,
                                &si,
                                &pi
                            )
        )
        {
            return  (   0);
        }

    CloseHandle (   pi.hThread);
    CloseHandle (   pi.hProcess);

    SetDebugErrorLevel  (   SLE_WARNING);


    while   (   WaitForDebugEvent   (   &dbgev, INFINITE))
            {
                dwStatus    =   DBG_CONTINUE;
                *acModule   =   ( char) 0;

                switch  (   dbgev.dwDebugEventCode)
                        {
                            case    EXCEPTION_DEBUG_EVENT       :


                                    wsprintf    (   acBuf,
                                                    "EXCEPTION_DEBUG_EVENT %s, code == 0x%08x at 0x%08x",
                                                    dbgev.u.Exception.dwFirstChance ? "first chance" : "second chance",
                                                    dbgev.u.Exception.ExceptionRecord.ExceptionCode,
                                                    dbgev.u.Exception.ExceptionRecord.ExceptionAddress
                                                );

                                    if  (   EXCEPTION_BREAKPOINT != dbgev.u.Exception.ExceptionRecord.ExceptionCode)
                                            dwStatus    =   DBG_EXCEPTION_NOT_HANDLED;


                                    break;


                            case    RIP_EVENT                   :

                                    wsprintf    (   acBuf,
                                                    "RIP_EVENT, error code == 0x%08x",
                                                    dbgev.u.RipInfo.dwError
                                                );

                                    break;

                            default:    break;
                        }

                printf  (   "PID:%u TID:%u %s\n",
                            dbgev.dwProcessId,  
                            dbgev.dwThreadId,
                            acBuf
                        );

                ContinueDebugEvent  (   dbgev.dwProcessId,  
                                        dbgev.dwThreadId,
                                        dwStatus
                                    );

                if  (   EXIT_PROCESS_DEBUG_EVENT    ==  dbgev.dwDebugEventCode)
                    {
                        break;
                    }
            }

(just a snippet)
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2555045
> 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
0
 
LVL 1

Author Comment

by:nv3prasad
ID: 2558753
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.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question