Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > Oracle Database, SQL, Application, Programming


Global exception handler


Reply
Views: 1297  
Thread Tools Rate Thread
  #1  
Old 08-11-2010, 02:17 PM
bholas bholas is offline
Award Winner
 
Join Date: Apr 2010
Posts: 4,977
Default Global exception handler

Although it's easy enough to catch errors (or exceptions) using "try / catch" 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 TApplication's "OnException" event -- which occurs when an unhandled exception occurs in your application, it only takes three (or so) easy steps get our own exception handler going:1. Declare your custom exception handler in your form's "public declarations" section. For example, if your form is named "Form1:"




{ Public declarations }

{ begin new code }
procedure MyExceptionHandler(
Sender : TObject; E : Exception );
{ end new code }
2. Define your exception handler in the "implementation" section:


procedure TForm1.MyExceptionHandler(
Sender : TObject; E : Exception );
var
wRetVal : Word;
begin
wRetVal := MessageDlg(
{
E.Message contains the
actual error message
we'll customize it a bit...
}

'ERROR: ' + E.Message,

mtError,
mbAbortRetryIgnore,
0
);

case wRetVal of
mrAbort:
begin
{ handle "Abort" here... }
end;


mrRetry:
begin
{ handle "Retry" here... }
end;

mrIgnore:
begin
{ handle "Ignore" here... }
end;

else
begin
{
handle "other" action here...
for example, if user choose to
close the message box without
clicking on any ****ons
}

end;
end;

{
you could also call the default
exception handler:

Application.ShowException( E );
}

end;
3. Finally, assign the newly created exception handler to your application's OnException event.


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

Reply With Quote
Reply

Tags
programming tips

New topics in Oracle Database, SQL, Application, Programming





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)