Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error handling - best practice 1

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
I was just interested to see other peoples approaches to error handling.

I usually use {try, except, finally}. This gets a bit tedious at times though. What method is the best to use?




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
There's Quick & Dirty, and there's Quick & Quality.
You trapping errors with try/except/finally goes in the Quick & Quality category.

HTH
TonHu
 
Thanks, but is this the right approach?

I am a VB programmer learning Delphi, so even though there is some similarity between the two, there is also a lot of differences.

With VB, I would tend to use, {try, catch, finally}, and {On Error GoTo ErrorHandler}.

Unfortunatley OnError is not supported in Delphi, and {try, finally] isn't trapping all of my errors (when attempting to connect to a database).

I use;

try
connection.open
except on E : Exception do
begin
connection.close
showmessage('Error')
end;
end;

What am I doing wrong?




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
Try...Finally isn't about catching errors, it's for making sure a block of code will alway run regardless of whether there's an error or not. I use Try...Finally in a couple of specific situations:

- I've opened a table or query and I want to make sure that it gets closed even if there's an error.

- I've created an object (usually a TStringList) and I want to make sure it's freed even if there's an error.

For actually exception handling, you'll use Try...Except. Also, the "On E:Exception" will give you access to the actual error message. For example:
Code:
except
  on e:Exception do
  begin
    MyDatabase.Rollback;
    ShowMessage('Error in MyFunction-' + e.Message);
  end;
end;

-D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top