Mega Code Archive

 
Categories / Delphi / Examples
 

A global exception handler

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;