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:

 

 

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.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
Leave your comment here:
Please type in the code:
photo Add a picture:

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.