whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

43 Visitors Online


 
...handle exceptions globally?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (10):  
     


{
  Although it's easy to catch exceptions using try/except blocks,
  some applications might benefit from having a global exception handler.
  For example, you may want your own global exception handler to handle "common"
  errors such as "divide by zero," "out of space," etc.
  Thanks to the TApplication's "OnException" event,
  which occurs when an unhandled exception occurs in your application,
  you can easily catch the unhandled exceptions.


  Obowhl es leicht ist, Fehler mittels try/except abzufangen,
  ist es manchmal von Vorteil, wenn die Anwendung eine eigene globale
  Fehlerbehandlungsroutine hat.
  Zum Beispiel zum Abfangen von Fehlern wie "Dividiert durch Null"
  oder "Zuwenig Speicherplatz" etc.
  Mit dem OnException Ereignis von TApplication ist es leicht,
  unbehandelte Fehler in der eigenen Anwendung abzufangen.
  Jede Exception, die nicht durch einen Try-Except-Block abgefangen wurde,
  gelangt dann automatisch in die Routine "MyExceptionHandler".
}

{1.

 Declare your custom exception handler:
 eklariere den eigenen globalen Exception Handler:
}


{ Public declarations }
procedure MyExceptionHandler(Sender : TObject; E : Exception );


{2.

 Define your exception handler in the "implementation" section:
 In den Implementations-Abschnitt kommt dieser Code:
}

procedure TForm1.MyExceptionHandler(Sender : TObject; E : Exception );
begin
  
MessageDlg('ERROR: ' + E.Message);
end;


{3.

 Assign the created exception handler to your application's OnException event.
 Weise dem OnException Ereignis den eigenen Exception Handler zu.
}

procedure TForm1.FormCreate(Sender: TObject);
begin
     
Application.OnException := MyExceptionHandler;
end;





 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners