DelphiFAQ Home Search:
General :: Programming :: Delphi
General Delphi related information.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

Creating a 'global' exception handler

Question:

Is it possible to write a 'global' exception handler where all exception will go to if there is no local exception handler? And if so, how can I get the line number and program unit name where the exception happened?

Answer:

Creating a global exception handler is no problem as the code fragment in the example below shows.
It is to my knowledge not possible to retrieve the line number and/or unit name in which the exception occured.

// application-global exception handler

 type
   TForm1 = class(TForm)
     procedure MyHandler(Sender: TObject; E : Exception);
     procedure FormCreate(Sender: TObject);
   end;
 
 procedure TForm1.MyHandler(Sender: TObject; E : Exception);
 begin
   // E.message is of type string
  toLogfile('Exception: ' + E.message);
   // either close the application or eat the exception
end;
 
 procedure TForm1.FormCreate(Sender: TObject);
 begin
   Application.OnException := MyHandler;
 end;
You don't like the formatting? Check out SourceCoder then!
Generated 16:01:43 on Jan 23, 2017