DelphiFAQ Home Search:

Creating a 'global' exception handler

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

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!

Comments:

2013-10-01, 15:48:08
crovax22@gmail.com  
How to catch even the already handled exceptions ?

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: