The address of the exception : an essential information
The best way to display the address of the last exception is to customize the
application's default exception handler : Application.OnException.
Delphi code starts here.
1 2 procedure TfmMain.FormCreate(Sender: TObject);
3 begin4 Application.OnException := OnAppliException;
5 ...
6 end;
7 8 procedure TfmMain.OnAppliException(Sender: TObject; E: Exception);
9 begin10 try11 E.message := E.message + ' at address $' + IntToHex(Integer(ExceptAddr), 8);
12 Application.ShowException(E);
13 except14 MessageDlg('Error in exceptions handler.', mtError, [mbOk], 0);
15 end;
16 end;
17 18 function TfmMain.DoDivide(Up,Down : Integer) : Integer;
19 begin20 Result := Up div Down;
21 end
Delphi code ends here.
In these few lines of code, the application first redirects its default exceptions
handler towards OnAppliException procedure. This new handler displays the exception
and its address.
Why is it important ?
Because the programer can find the line of code which raised the exception with the
address of the exception. In our example, he has to
- launch Delphi and execute the application,
- open 'Execution error' option in Delphi's 'Search' menu,
- enter $00458833.
Then Delphi shows the line
Result := Up Div Down;
The error line is located in the sources and the debugging task can really begin...
Here, we have to modify DoDivide function by adding a test on Down parameter.
The address of the exception is so an important information that it should always
be displayed in error messages ! Don't hesitate to add these few lines of code into
your applications : it will already be of a great help for your debugging sessions.
Robert Sampy