Name
Try Keyword
Syntax
tryStatements... finallyStatements... end; tryStatements... exceptStatements... end; tryStatements... exceptHandlers... end;
Description
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.
Tips and Tricks
Use
try-finallyto free temporary objects and other resources and to perform related cleanup activities. Typically, you should not need ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access