Stack trace in application.onexception

delphi package - automated exception handling
Post Reply
tr@logihouse.dk
Posts: 2
Joined: Fri Sep 21, 2018 11:15 am

Stack trace in application.onexception

Post by tr@logihouse.dk »

Hello Madshi

Thank you for an excellent product! It works very well.

I would like to get stacktrace in Application.OnException for unhandled exceptions:

uses MadStackTrace;

procedure Tform1.AppException(Sender: TObject; E: Exception);
var s : string;
begin
s := MadStackTrace.StackTrace(true);
// s = '' 15 TForm1.AppException'#$D#$A' ... initialization'
// had hoped to see:
// S= '' 22 Tform1.Button1Click ...'
end;

It seems, that the stack is cleared prior to AppException is called?

I Know, I can make the madshisystem handle unhandled exceptions, but I would like to show the same error dialogue, that I use for handled exceptions (here I can get the correct stack dump)
How do I get the stack dump un unhandled exceptions?

Best regards
Thomas Riedel
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: Stack trace in application.onexception

Post by madshi »

Hi Thomas,

Application.OnException is hooked by madExcept and should not even be called at all. I wonder why/how it's called for you!?? :o

Anyway, you can use madExcept.RegisterExceptionHandler() to be notified about unhandled exceptions. Please be careful with the parameters of that function. They define in which thread context your callback is called. If you want to show your own VCL dialog, you need to be aware of that the VCL is not thread safe. madExcept's own dialogs are all done without VCL, using pure win32 APIs, to make them perfectly thread safe.
tr@logihouse.dk
Posts: 2
Joined: Fri Sep 21, 2018 11:15 am

Re: Stack trace in application.onexception

Post by tr@logihouse.dk »

It worked partially because Mad except was not linked into the application.
All threads in my system are inside try .. except, so the dialog does not need to be thread safe in this case.
Can you link to an example with registerexceptionhandler, where where I can see how to get the stack?

Best regards
Thomas
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: Stack trace in application.onexception

Post by madshi »

We're talking about unhandled exceptions, no? Those are handled by madExcept in a secondary thread, so your RegisterExceptionHandler() callback can also be called in the context of a secondary thread. But you can define in which thread context you want your callback to be called, so RegisterExceptionHandler() parameters.

There is a little sample code at the bottom of this page:

http://help.madshi.net/HowToUseMadExcept.htm

However, it runs in a secondary thread because it doesn't need VCL.
Post Reply