Mega Code Archive

 
Categories / Delphi / Examples
 

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;