1. Computing

On Reraising Exceptions in Delphi Exception Handling

By March 13, 2012

Follow me on:

in Delphi Exceptions :: This is the second (first here) article in a series I've devoted to exceptions and handling them. An exception is a special conditions in your code requiring special handling.

A discussion on how to handle and what happens when you handle exceptions in your Delphi code is provided as the intro article. This time, we'll see how you can explore exceptions being raised and handled and how to properly re-raise exceptions.

Read the full article to learn how to Re-Throw Exceptions In Delphi Exception Handling Blocks

Related:

Comments
March 13, 2012 at 7:47 am
(1) Konstantin says:

Hello! I often use next code to add additional information:

try
raise Exception.Create(‘Error Message’);
except
on E: Exception do
begin
E.Message := E.Message + ‘: no details’;
raise;
end;
end;

Do you think that this code can bring Access Violation?
Thanks.

March 13, 2012 at 8:07 am
(2) Stefan says:

@Konstantin:
Why on earth should the exception handling add additional informations? The part that raises the exception is responsible for doing that – either by raising specific exceptions and/or providing a more detailed error message.

March 13, 2012 at 8:31 am
(3) Zarko Gajic says:

@Konstantin: No this will not raise an AV, but “raise E;” would.

March 13, 2012 at 6:02 pm
(4) Anton says:

Konstantin: don’t listen to anybody, adding extended information into E.Message is a very nice patterm and is very widely used

March 13, 2012 at 6:20 pm
(5) Rick Peterson says:

Stephan there are times when you want to display a little more information to the end user instead of cryptic messages that another tool may leave. Good example are the ODBC messages. Some of them are just codes or states…and don’t tell the end user the problem.

One thing I do notice that beginner programmers handle to many exceptions and should only handle them when they can truly add something useful.

I use try finally more often than try except…Transaction handling is an example that is easier to read if handled in a try Finally, think most programmers use a try except.

procedure Tdm.HandleExecute;
var
a_Commit: boolean;
begin
a_Commit:= False;//set myself in error state
FConnection.BeginTrans;
try
DoStuffHere;
FQuery.Exec;
a_Commit:= True;//if I get here everything is good;
finally
if a_Commit then
FConnection.CommitTrans
else
FConnection.RollbackTrans
end;
end;

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2014 About.com. All rights reserved.