17

I'd like to display a stack trace in an error dialog in Delphi 2007 (Win32).

Ideally, I'd like something like this:

try
  //do something
except on e : exception do
  begin
    //rollback a transaction or whatever i need to do here       
    MessageDlg('An error has occurred!' + #13#10 +
                e.Message + #13#10 +
               'Here is the stack trace:' + #13#10 +
               e.StackTrace,mtError,[mbOK],0);
  end;  //except
end;  /try-except

And for the output to be like the Call Stack in the IDE:

MYPROGRAM.SomeFunction
MYPROGRAM.SomeProcedure
MYPROGRAM.MYPROGRAM
:7c817067 kernel32.RegisterWaitForInputIdle + 0x49
JosephStyons
  • 57,317
  • 63
  • 160
  • 234
  • 1
    OT: I'd use [`sLineBreak`](http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/System_sLineBreak.html) instead of hardcoded `#13#10`. – Uli Gerhardt Jan 05 '16 at 08:09

3 Answers3

23

madExcept has a method StackTrace (in unit madStackTrace) that does that.

JEDI Code Library offers similar functionality in unit JclDebug.

gabr
  • 26,580
  • 9
  • 75
  • 141
  • 5
    madExcept is not free for commercial use. JclDebug is free and open source. There also is EurekaLog (http://www.eurekalog.com/) – Lars Truijens Nov 03 '08 at 16:23
  • 1
    >>> Eurekalog uses the JEDI code That is not 100% correct. Yes, EL includes parts of JCL code, but only for the supporting JDBG format and similar issues. The engine itself is written from scratch. Also EL uses its own format for debug-information (not JDBG). – Alex Apr 25 '09 at 11:29
  • 2
    For a step-by-step to using JEDI's JclDebug, I have found this useful, http://robstechcorner.blogspot.com/2009/04/finding-hard-to-reproduce-errors.html [simple steps section] – jasonpenny Feb 18 '10 at 13:42
  • I guess that would be great an example on how to do that and not ide experts to do the job – Carlos B. Feitoza Filho Feb 13 '12 at 02:52
10

We use Exceptional Magic and it works really well for us. With it you can do something like this:

try
    raise Exception.Create('Something bad happened...');
except
    on e: Exception do begin
        CallStack := TStringList.Create;
        try
            ExceptionHook.LogException; // Logs call stack
            ExceptionHook.CallStack.Dump(CallStack);
            ShowMessage(CallStack.Text);
        finally
            CallStack.Free;
            end;
        end;
    end;

This yields a pretty detailed call stack:

Exception 'Exception' in module BOAppTemplate.exe at 003F3C36
Something bad happened...

Module: BOAppUnit, Source: BOAppUnit.pas, Line 66
Procedure: MyProcedure

Call stack:
:007F4C36 [BOAppTemplate.exe] MyProcedure (BOAppUnit.pas, line 66)
:7C812AFB [kernel32.dll]
:007F4C36 [BOAppTemplate.exe] MyProcedure (BOAppUnit.pas, line 66)
:00404DF4 [BOAppTemplate.exe] System::__linkproc__ AfterConstruction
Recursive call (2 times):
:007F4C36 [BOAppTemplate.exe] MyProcedure (BOAppUnit.pas, line 66)
:007F4CE6 [BOAppTemplate.exe] MyProcedure (BOAppUnit.pas, line 79)
:007F4D22 [BOAppTemplate.exe] Boappunit::TBOAppForm::Button1Click (BOAppUnit.pas, line 82)
:004604C2 [BOAppTemplate.exe] Controls::TControl::Click
:004487FB [BOAppTemplate.exe] Stdctrls::TButton::Click
:004488F9 [BOAppTemplate.exe] Stdctrls::TButton::CNCommand
:0045FFBA [BOAppTemplate.exe] Controls::TControl::WndProc

Exceptional Magic is only $25 without the source, so it's relatively cheap. Hope that helps!

Martin Binder
  • 363
  • 3
  • 12
5

You may be interested in this article: "New Exception class in Delphi 2009 and above".

jep
  • 747
  • 6
  • 25
Alex
  • 5,477
  • 2
  • 36
  • 56