Tuesday, March 1, 2011

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 buttons
}

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;
 
Source : http://www.chami.com/tips/delphi/011497D.html

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum