Using raise without MadExcept getting involved

delphi package - automated exception handling
paulcoshott
Posts: 9
Joined: Fri Aug 13, 2004 9:47 am
Location: Australia

Using raise without MadExcept getting involved

Post by paulcoshott »

Hi,

I am trying out MadExcept and all was looking good until I noticed MadExcept gets involved if I use raise. I declare an exception class as follows :

type
TE_Error = class(Exception);


And then in an OnBeforePost event I may check the validity of some values. For example :

if tCodesCode.AsString = '' then begin
eCode.SetFocus;
raise TE_Error.Create('A location code must be entered.');
end; {if}
if tCodesDescription.AsString = '' then begin
eDescription.SetFocus;
raise TE_Error.Create('A location code description must be entered.');
end; {if}


Any idea how to get MadExcept to ignore this code, but still have the raise display the error and cancel the post as it does without MadExcept.

Cheers,
Paul
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Please check FAQ #1 in the documentation. Does that help?
paulcoshott
Posts: 9
Joined: Fri Aug 13, 2004 9:47 am
Location: Australia

Using raise without MadExcept getting involved

Post by paulcoshott »

Hi,

yes it helps, but I'm not sure how to use the unit in the overall context of my program. Where should I link it in etc. I define my exception class in each unit that I use it. Do I need the unit in all these or only in the main unit ?

Sorry, I'm a bit lost at the moment.

Cheers,
Paul
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

You need to do that once in your project, it doesn't matter where. It should be before the exception is raised, of course.
paulcoshott
Posts: 9
Joined: Fri Aug 13, 2004 9:47 am
Location: Australia

Post by paulcoshott »

Sorry, I'm not sure how to use the unit. Do I just add it to my main units uses clause ?

And what do I insert where the (...) is in the ExceptionFilter procedure ?

Is the exceptObject my exception class object that I decared ?

Thanks,
Paul
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

paulcoshott wrote:I'm not sure how to use the unit. Do I just add it to my main units uses clause ?
Yes, add it to the uses clause of any of your units or to the uses clause of your project (dpr). You can also rip the code and paste it into one of your units.
paulcoshott wrote:And what do I insert where the (...) is in the ExceptionFilter procedure ?
See code below.
paulcoshott wrote:Is the exceptObject my exception class object that I decared ?
exceptObject is the Exception object. It can be your exception class or anything else, e.g. EAccessViolation.

Code: Select all

procedure ExceptionFilter(frozen          : boolean;
                          exceptObject    : TObject;
                          exceptAddr      : pointer;
                          crashedThreadId : dword;
                          var bugReport   : string;
                          var screenShot  : string;
                          var canContinue : boolean;
                          var handled     : boolean);
begin
  if (exceptObject <> nil) and (exceptObject is TE_Error) then begin
    // here you can do with the exception whatever you like
    // e.g. you can show a standard information box
    // please note that whatever you do here should be thread safe
    // except if you told madExcept to call your handler in the context of
    // the main thread
    MessageBox(0, pchar(EDBEditError(exceptObject).Message), 'Error...', MB_ICONERROR);

    // tell madExcept that you've already taken care of the exception
    // as a result madExcept will not show its exception box this time
    handled := true;
  end;
end;
paulcoshott
Posts: 9
Joined: Fri Aug 13, 2004 9:47 am
Location: Australia

Post by paulcoshott »

Fantastic. Thanks for all your help. I'll give it a try and see how things go.

Cheers,
Paul
paulcoshott
Posts: 9
Joined: Fri Aug 13, 2004 9:47 am
Location: Australia

Post by paulcoshott »

Hi,

I am able to compile the following code, and while debuging, the code is being executed, but it is not handling the error, and the MadExcept window is displayed. In the bug report, the Exception Class is listed as TE_Error. The unit is included in my main unit.
Any idea what I am doing wrong ?

==========================================
unit ExceptionFilterUnit;

interface

uses
Windows, SysUtils;

type
TE_Error = class(Exception);

implementation

uses madExcept;

procedure ExceptionFilter(frozen : boolean;
exceptObject : TObject;
exceptAddr : pointer;
crashedThreadId : dword;
var bugReport : string;
var screenShot : string;
var canContinue : boolean;
var handled : boolean);
begin
if (exceptObject <> nil) and (exceptObject is TE_Error) then begin
MessageBox(0, pchar(TE_Error(exceptObject).Message), 'Error...', MB_ICONERROR);

CanContinue := True;
Handled := True;
end;
end;


initialization
RegisterExceptionHandler(ExceptionFilter, false);

end.
===========================================

Cheers,
Paul
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

You have declared TE_Error in this unit. And I'm sure you have it declared somewhere else, too. So you have two declarations of TE_Error and they are not identical! If you ask "if exceptObject is TE_Error" you're asking "if exceptObject is ExceptionFilterUnit.TE_Error", which is surely not what you want to do. There are 2 ways to solve this:

(1) Only declare TE_Error once. Import the unit in which it is defined in your ExceptionFilterUnit. Or put the code of the ExceptionFilterUnit into the unit where you've defined TE_Error.

(2) Or change it like this:

if (exceptObject <> nil) and (exceptObject.ClassName = 'TE_Error') then begin

This works only for TE_Error exceptions, but not for exception classes which are descendants of TE_Error. So I'd recommend to use (1).
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

P.S: Setting "cantContinue" to true doesn't have any effect. If madExcept tells you that the program can't continue there's nothing you can do. The parameter is a "var" parameter, so you can set it to false. But setting it to true has no effect.
tojo77
Posts: 3
Joined: Mon Aug 23, 2004 12:50 pm

SAME TOPIC

Post by tojo77 »

hi,
I want to do exactly the same (custom Exception-class) and it worked fine until I updated to madExcept 2.7c. Now it takes quite a while before *any* exception is handled by my custom madExceptHandler!
I guess madExcept is collecting all the new hardware information etc. but in case of my custom Exception-class I don't need any additional information!

Is there a solution?

thx,
Tom
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

The hardware information shouldn't take that much time. Please try to uncheck the screenshot functionality. I guess that's what is consuming the time.
tojo77
Posts: 3
Joined: Mon Aug 23, 2004 12:50 pm

Post by tojo77 »

thats probably true. but the screenshot is already unchecked! can I do it in the source code as well?
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

If you've already unchecked the screenshot, then it shouldn't be the guilty one. Hmmm... What else can it be? In which thread are the exceptions raised? Can you create a little demo for me which reproduces this big delay?

Thanks!
paulcoshott
Posts: 9
Joined: Fri Aug 13, 2004 9:47 am
Location: Australia

Post by paulcoshott »

Hi,

I had the exact same problem. Sometimes I would get the exception (either the madExcept one or my own exception) in 3 to 4 seconds, and sometimes it could take over 30 seconds.

I just went through and unticked almost everything and it is instant again. I will start adding bits and let you know what I find.

Paul
Post Reply