tags:

views:

1150

answers:

3

Hi 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;
+5  A: 

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=
Mihai Limbășan
+17  A: 

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).

Andreas Hausladen
Hi Andy - welcome to SO...!
Roddy
Thanks - I have just got Eurekalog - it works well
Charles Faiga
+4  A: 

Mostly related: Exception Handling in Delphi.

gabr