Charles Faiga
Charles Faiga

Reputation: 11753

How can I find out which procedure threw an exception in Delphi?

I am using Delphi TApplication.OnException Event to catch unhandled exceptions

This works well but does not give sufficient information about where the exception happened i.e. ‘Catastrophic failure’

How can I find out which procedure made the error happened?

procedure TFrmMain.FormCreate(Sender: TObject);
begin
  Application.OnException := MyExceptionHandler;
end;

procedure TFrmMain.MyExceptionHandler(Sender : TObject; E : Exception );
begin
  LogException (E.Message);     
  Application.ShowException( E );
end;

Upvotes: 12

Views: 5903

Answers (3)

gabr
gabr

Reputation: 26830

Mostly related: Exception Handling in Delphi.

Upvotes: 7

Andreas Hausladen
Andreas Hausladen

Reputation: 8141

You can get the memory address where the exception was thrown by using the ExceptAddr variable (System unit). But if you want a stack trace you could use one of the 3rdParty tools MadExcept, EurekaLog or the open source JCLDebug (part of the JCL).

Upvotes: 25

Mihai Limbășan
Mihai Limbășan

Reputation: 67576

The simplest and quickest way would be to use the JCL exception and debugging support. After installing the JCL, make sure to insert the debug symbols into the binary (Projects -> JCL debug expert -> Insert JDBG data for this binary -> Enabled) and add a JCL exception dialog to the project (File -> New... -> Dialogs -> Exception dialog).

If the JCL installer fails to add that dialog to the object repository and it doesn't appear (happened to me a few times), either add it manually by copying the .pas and .dpr file from jcl-install-dir\experts\debug\dialog into your project and adding them manually, or close Delphi, edit %DELHPIDIR%\bin\delphi32.dro in a text editor and add something like this to it (adjusting the paths of course:)

[P:\DELPHI11\EXTERNALLIB\JCL\EXPERTS\DEBUG\DIALOG\EXCEPTDLG]
Type=FormTemplate
Name=Exception Dialog
Page=Dialogs
Icon=P:\DELPHI11\EXTERNALLIB\JCL\EXPERTS\DEBUG\DIALOG\EXCEPTDLG.ICO
Description=JCL Application exception dialog
Author=Project JEDI
DefaultMainForm=0
DefaultNewForm=0
Ancestor=

[P:\DELPHI11\EXTERNALLIB\JCL\EXPERTS\DEBUG\DIALOG\EXCEPTDLGMAIL]
Type=FormTemplate
Name=Exception Dialog with Send
Page=Dialogs
Icon=P:\DELPHI11\EXTERNALLIB\JCL\EXPERTS\DEBUG\DIALOG\EXCEPTDLGMAIL.ICO
Description=JCL Application exception dialog
Author=Project JEDI
DefaultMainForm=0
DefaultNewForm=0
Ancestor=

Upvotes: 9

Related Questions