Name

Try Keyword

Syntax





try Statements... finally Statements... end;

try Statements... except Statements... end;

try Statements... except Handlers... 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-finally to free temporary objects and other resources and to perform related cleanup activities. Typically, you should not need ...

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.