Both custom exception handler AND MadExcept dialog shown

delphi package - automated exception handling
Post Reply
dnote
Posts: 17
Joined: Fri May 07, 2004 8:02 am

Both custom exception handler AND MadExcept dialog shown

Post by dnote »

I'm having yet another problem with my custom exception handler. If an exception occurs during application initialization, both my custom handler and the madexcept dialog are show. If the exception is raised later on, f.e. in a buttonclick, everything works ok.

Here's the example code:

Code: Select all

unit Unit1;

interface

implementation

uses
  Windows, Dialogs, SysUtils, MadExcept;

procedure MyExceptEventHandler(frozen          : boolean;
                              exceptObject    : TObject;
                              exceptAddr      : pointer;
                              crashedThreadId : dword;
                              var bugReport   : string;
                              var screenShot  : string;
                              var canContinue : boolean;
                              var handled     : boolean);
begin
  ShowMessage(0, 'Test', 'Test', MB_ICONERROR);
  handled := True;
end;

initialization
  RegisterExceptionHandler(MyExceptEventHandler, True);
  raise Exception.Create('');
  // after the above call, both my messagebox and both the mad dialog are shown

end.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

I know, it seems to be odd. But actually it's more or less intended behaviour. The problem is this: By setting "handled := true" you're telling madExcept to now show its own exception box and to continue with the normal program flow. But after an exception in the program initialization there's no way to continue with the normal program flow! So madExcept doesn't know what to do. Of course it can do as you like, namely not showing the madExcept style exception box. What what to do next? Continuation of the program is not possible. So what shall madExcept do now?

Do you see the problem?

madExcept internally has some code like this:

Code: Select all

  originalCanContinue := canContinue;
  CallAllRegisteredExceptionHandlers(canContinue, handled, ...);
  if not originalCanContinue then begin
    handled     := false;
    canContinue := false;
  end;
In other words: If madExcept knows that continuation of the program is not possible after the exception was handled, madExcept doesn't allow any registered exception handler to set "handled := true" or "canContinue := true".

You can solve the problem by changing your code a bit:

Code: Select all

procedure MyExceptEventHandler(frozen          : boolean;
                              exceptObject    : TObject;
                              exceptAddr      : pointer;
                              crashedThreadId : dword;
                              var bugReport   : string;
                              var screenShot  : string;
                              var canContinue : boolean;
                              var handled     : boolean);
begin
  ShowMessage('Test');
  if canContinue then
    handled := True
  else
    ExitProcess(0);
end;
dnote
Posts: 17
Joined: Fri May 07, 2004 8:02 am

Post by dnote »

Thanks! This works perfectly...
Post Reply