1

I'm using Delphi 7 and in an attempt to handle all the possible exceptions being thrown during the run of the program. I used Application.OnException := HandlerProcedure; to handle exceptions but when exception occurs, HandlerProcedure never gets called. In order to assure if it really works, I raised exception after I assigned Application.OnException as below:

Application.OnException := HandlerProcedure;
raise Exception.Create('Exception');

and defined HandlerProcedure as:

procedure TFormMain.HandlerProcedure(Sender: TObject; E: Exception);
begin
    ShowMessage('Exception.');      
    Exit;
end;

But HandlerProcedure never gets called. How can I make it handle all the exceptions?

Aarohi S
  • 157
  • 2
  • 2
  • 9

3 Answers3

13

If you want to intercept ALL exceptions, you need to implement a RTLUnwindProc low-level procedure.

This is a bit low-level (e.g. it needs asm skills), so you should better rely on existing code. See this stack overflow question. I even put some reference code (including low-level asm, working with Delphi 7 and later under Win32) in my own answer.

Community
  • 1
  • 1
Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • 1
    Most likely this poor advice. I expect that all that's needed is for asker to fix their bug so the OnException is called. At best this answer is misleading. – David Heffernan Aug 29 '13 at 12:19
  • 3
    @DavidHeffernan OP wrote he was "*in an attempt to handle all the possible exceptions being thrown during the run of the program*"... sounds like if my answer fits with this question, doesn't it? – Arnaud Bouchez Aug 29 '13 at 12:28
  • 4
    Asker surely doesn't understand the nuance that you are inferring – David Heffernan Aug 29 '13 at 12:38
  • 3
    Down voting because of what you think the OP is potentially nuancing is pretty unfair. Googling about 'handle all exceptions of a Delphi program' will certainly point to this question. I'm sometimes fed up of SO down voters. – Arnaud Bouchez Aug 29 '13 at 19:20
  • 1
    I personally think you should get +1 for trying to put useful information out there. +1 from me. – Warren P Aug 30 '13 at 01:53
3

Something is wrong in your code. The example from Embarcadero's website is working perfect.

{
In addition to displaying the exception message, which 
happens by default, the following code shuts down the 
application when an exception is not caught and handled.  
AppException should be declared a method of TForm1.
}
procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnException := AppException;
end;

procedure TForm1.AppException(Sender: TObject; E: Exception);
begin
  Application.ShowException(E);
  Application.Terminate;
end; 

procedure TForm1.Button1Click(Sender: TObject);
begin
  raise EPasswordInvalid.Create('Incorrect password entered');
end;

Also good practices on handling errors on Delphi are described here. In order to further investigate the problem you have, you should take a look at this https://stackoverflow.com/questions/1259563/good-os-delphi-exception-handling-libraries

Community
  • 1
  • 1
RBA
  • 12,337
  • 16
  • 79
  • 126
0

If you are using a third party exception handler such as madExcept, Application.OnException no longer fires. You must instead code TMadExceptionHandler.OnException event or directly call RegisterExceptionHandler.

fullerm
  • 406
  • 1
  • 8
  • 23