Solved

Hook For All exceptions in my application...

Posted on 2000-02-23
11
230 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 179
Define unique primary key 9 101
unable to delete all specified values regedit 38 912
maxBlock challenge 30 151
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
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.

752 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