Previous Topic

Next Topic

Book Contents

Book Index

Try...Catch...Finally...End Try statement

This is a compound multi line statement designed to allow exception handling for blocks of statements.

Syntax

Try
  [TryStatements] ]
[Catch [Variable]
  [CatchStatements] ]
[Finally
  [FinallyStatement] ]
End Try

Part

Description

Variable

Optional variable to place exception object in if caught.

TryStatements

Statements for which exception handling is to be performed.

CatchStatements

Statements to be performed if an exception is caught. Exceptions can be generated by the Throw statement above or by the application.

FinallyStatements

Statements guaranteed to be performed after the Try statements irrespective of an exception or exception handling, usually used to clean up resource allocations, for example, forms created, files created.

Notes:

The Exception Object has the following properties:
Message            The text of the exception created.
SourcePos         The file line and character position of where the exception originated.
ExceptionClass  The Delphi Object class for the exception.

The following is an example of the exception handling code in action (for Accredo Saturn):

Try
  Print "Main block"
  Print 1/0
Catch E
  Print E.Message
  Print E.SourcePos
  Print E.ExceptionClass
Finally
  Print "Finally"
End Try

Will produce output:

Main block
Floating point division by zero
Z:\AccredoSaturn\Scripts\TryTest.pfs Line: 3 Char: 10)
EReportLanguage
Finally