DelphiFAQ Home Search:

A global exception handler

 

comments2 comments. Current rating: 5 stars (2 votes). Leave comments and/ or rate it.

You may want your own global exception handler to handle "common" errors such as "out of space," etc.
If you try to create a global exception handler, you will experience that surrounding the 'Application.Run' command in the .dpr with a try...except block fails.

You will redirect TApplication's "OnException" event to your main form's exception handler "MyExceptionHandler". The redirection is done in FormCreate, similar to assigning Application.OnHint:

TForm = class()..
  { public declarations }
    procedure MyExceptionHandler(Sender : TObject; E : Exception);
  end;  

{ .. }

procedure TForm1.MyExceptionHandler(
  Sender : TObject; E : Exception );
var
  wRetVal : Word;
begin
  wRetVal := MessageDlg('ERROR: ' + E.message,
               mtError, mbAbortRetryIgnore, 0);

  case wRetVal of
  mrAbort:; { handle "Abort" here... }
  mrRetry:; { handle "Retry" here... }
  mrIgnore:;{ handle "Ignore" here... }
  end;

  { or call the default exception handler:
     Application.ShowException(E); }
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnException := MyExceptionHandler;
end;
You don't like the formatting? Check out SourceCoder then!
Content-type: text/html

Comments:

2006-04-05, 05:00:29
[hidden] from India  
rating
This is very use ful topic for handling exception globally by simply adding pne procrdure.
its good programming practice.
2006-04-05, 05:01:38
anonymous from India  
rating
This is very use ful topic for handling exception globally by simply adding pne procrdure.
its good programming practice.

 

 

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: