Try Keyword
tryStatements
... finallyStatements
... end; tryStatements
... exceptStatements
... end; tryStatements
... exceptHandlers
... end;
The try
keyword introduces a
try
-except
statement or a
try
-finally
statement. The two
statements are related but serve different purposes.
The try
-finally
statement is
used to manage memory and other resources. It performs the statements
first in the try
part of the statement, then in
the finally
part of the statement. The statements
in the finally
part are executed no matter how
control leaves the try
part: exception, returning
from a subroutine with the Exit
procedure, or
exiting a loop with the Break
procedure.
The try
-except
statement
handles errors and exceptional conditions. It first performs the
statements in the try
part of the statement. If an
exception is raised, control transfers to the
except
part of the statement, where Delphi
searches for a matching exception handler. If the
except
part contains plain statements, Delphi
executes those statements, and then control continues after the end
of the try
-except
statement. If
the except
part contains one or more exception
handlers (which start with the on
directive),
Delphi searches for a matching handler. If it cannot find a matching
handler, control continues with the next
try
-except
statement in the
call stack.
Use try
-finally
to free
temporary objects and other resources and to perform related cleanup
activities. Typically, you should not need more than one
try
-finally
statement in a
subroutine or
method.
Use try
-except
to handle
exceptional cases. In utilities and reusable classes, you should
rarely use try
-except
. Instead,
an application uses try
-except
to catch specific exceptions and do something useful with them, such
as log them in an error log, send email to the vendor, or create a
friendly dialog box.
Low-level code might use
try
-except
to catch a low-level
exception, add information, and raise a higher-level exception.
// Copy a file. procedure CopyFile(const DestFile, SourceFile: string); const ReadFlags = fmOpenRead or fmShareDenyWrite; resourcestring sSourceError = 'Cannot open source file: %s'; sDestError = 'Cannot open destination file: %s'; sCopyError = 'Cannot copy %1:s to %2:s: %0:s'; var DestStream, SourceStream: TStream; begin DestStream := nil; SourceStream := nil; try try SourceStream := TFileStream.Create(SourceFile, ReadFlags); DestStream := TFileStream.Create(DestFile, fmCreate); DestStream.CopyFrom(SourceStream); except // EFCreateError and EFOpenError derive from EStreamError, // so check the derived classes first. Raise a new exception // that provides additional information. If any other kind // of exception is raised, the exception propagates to the // caller and its caller, until Delphi finds another exception // handler. on Ex: EFCreateError do raise EFCreateError.CreateFmt(sDestError, [Ex.Message]); on Ex: EFOpenError do raise EFOpenError.CreateFmt(sSourceError, [Ex.Message]); on Ex: EStreamError do raise EStreamError.CreateFmt(sCopyError, [Ex.Message, SourceFile, DestFile]); end; finally // Even if the try-except raises an exception, the finally // part executes, so the stream objects can be freed. DestStream.Free; SourceStream.Free; end; end;