Solved

Hook For All exceptions in my application...

Posted on 2000-02-23
11
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

615 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