[Lazarus] How to use ExceptProc and application.CaptureExceptions

Alexander Grau alex at grauonline.de
Tue Feb 16 01:22:30 CET 2010


I'm answering my own question since I got it solved now by setting an 
ExceptionHandler using Application.AddOnExceptionHandler:

    application.AddOnExceptionHandler(@MyExceptionHandler);

This will capture exceptions, however a stack trace with line numbers 
won't work yet because the default handler is still called and will 
output a stack trace with line info, but the 'lineinfo' procedure cannot 
generate a 2nd, 3rd etc. stack trace with line info (for some unknown 
reason).

The default handlers can be disabled setting a flag:

Application.Flags=[AppNoExceptionMessages]

That did the trick. Then the stack trace is available with line info for 
at least one time!

Cheers,
Alexander

Alexander Grau schrieb:
> Hello,
>
> I'm trying to turn off application exception capturing (so I can send 
> exception traces via HTTP in bug reports):
>
> 1) In (the first loaded unit of) a console app, I did override 
> ExceptProc (see code at bottom) - my own handler is called (as 
> expected) on a EDivByZero exception.
>
> 2) In my Lazarus LCL app (SVN 23483M,  LCL/Win32/i386, fpc 2.4.0), I 
> did the same like in the console app - additionally, I did set 
> Application.CaptureExceptions to 'false':
>
>  Application.Initialize;
>  Application.CreateForm(TForm1, Form1);
>  application.CaptureExceptions:=false;
>  Application.Run;
>
> Now, when a EDivByZero is raised, my own handler is *not* called - 
> instead the default TApplication.HandleException is called - To my 
> surprise...
> a) When raised before Application.Run, my own handler is called (as it 
> should).
> b) When I debug Application.Run, it correctly uses the case without 
> exception handling(!) - that is, it correctly detects the 
> CaptureExceptions change.
> c) When I read ExceptProc right before the exception, it correctly has 
> the value of my own handler(!)
>
> Though, my own handler is never called within Application.Run - and I 
> have no idea why...
>
> Any help or ideas are welcome!
>
> Thanks,
> Alexander
>
>
> ---------------Console output------------------------
>
> install
> application.CaptureExceptions=0
> TApplication.HandleException Division by zero
>  Stack trace:
>  $00420413  CRASHME,  line 39 of unit1.pas
>  $0042047A  TFORM1__BUTTONCRASHMECLICK,  line 46 of unit1.pas
>  $004C6234  TCONTROL__CLICK,  line 2294 of ./include/control.inc
>  $004FA26F  TBUTTONCONTROL__CLICK,  line 72 of 
> ./include/buttoncontrol.inc
>  $004FA745  TCUSTOMBUTTON__CLICK,  line 164 of ./include/buttons.inc
>  $004FACA1  TBUTTON__CLICK,  line 331 of ./include/buttons.inc
>  $004FA10A  TBUTTONCONTROL__WMDEFAULTCLICKED,  line 26 of 
> ./include/buttoncontrol.inc
>  $0040A84A
>  $004BCE72  TWINCONTROL__WNDPROC,  line 5102 of ./include/wincontrol.inc
>  $0050FCC7  DELIVERMESSAGE,  line 111 of lclmessageglue.pas
>  $004DB6B0  WINDOWPROC,  line 2621 of win32callback.inc
>  $77D18709
>  $77D187EB
>  $77D1B743
>  $77D1B7AB
>  $773C6FF6
>  $773C70D8
>
> -----------My exception handler---------------------
>
> procedure MyExceptionHandler(
>  Obj : TObject;       // ExceptObject
>  Addr: Pointer;       // ExceptAddr
>  FrameCount: Longint; // ExceptFrameCount
>  Frames: PPointer     // ExceptFrames
> );
> begin
>  writeln('MyExceptionHandler');
>  ShowException(obj, addr);
> end;
>
>
> initialization
>  writeln('install');
>  exceptproc := @MyExceptionHandler;
>
>
>
>


-- 

Mit freundlichen Grüßen/Best regards
Alexander Grau

Grau GbR
Hardware & Software Solutions
Eschenweg 12
32609 Huellhorst
Germany

www.grauonline.de
Tel: +49 (0) 5741 2301259
Fax: +49 (0) 5741 235530
E-Mail: info at grauonline.de





More information about the Lazarus mailing list