Try...Catch...Finally Statement

Exception handling statements enable you to bypass the default error handling mechanism and control the sequence of events following an error. In Intent language, this is done through the Try...Catch...Finally statement.

A Try clause consists of a single Try block followed by any number of optional Catch blocks and a single Finally block. The syntax of a simple Try...Catch...Finally statement follows:

Try
   [<statements>]
[Catch [identifier]
   [<statements>]]
[Finally
   [<statements>]]
End Try

The statements contained by the Try block are executed. If an exception occurs during evaluation, the Catch blocks are inspected to see if they match the exception. If a Catch block matches the exception. the statements in that Catch block are evaluated. When the Try block or Catch block is exited, then a Finally block is executed, whether or not an exception has occurred and been handled.

Note: A Try...Catch...Finally statement must contain at least one Catch block or a Finally block.

Catch Clause

When an exception is generated in Try block, the Catch blocks, if present, are sequentially examined to determine if one can handle the exception. Only the first Catch which matches will have its statements executed.

Syntax:

Catch [identifier]
   [<statements>]]

The identifier specified in a Catch clause represents the Design of the Error Part, which must derive from the system Design Error. Thus, Catch Error will match any exception.

Finally Clause

A Finally block is executed when a Try block is exited, or when a selected Catch block is exited. A Finally block is optional if there is at least one Catch block.

Syntax:

[Finally
   [<statements>]]

Example

In the following example, a statement in the Try block compares the value of the load parameter to a limit. If the value of the load does not exceed the limit, the Try block is exited, a statement in the Finally block prints a system message, and the loop repeats. If the value of the load exceeds the limit, an exception is generated and program execution is halted. However, before the program is halted, a statement in the Catch block prints a system message before halting execution.

Parameter Rule unitWeight As Integer = 5

Rule load As Number
   Dim counter As Integer = 1
   Dim limit As Number = 25.0
   Dim strItem As String = " item."
   Do
      If counter > 1 Then
         strItem = " items."
      End If
      load = load + unitWeight
      Try
         If load > limit Then
            Error()
         End If
      Catch Error
         printValue("ERROR: Load limit exceeded! Load = " + stringValue(load) + _
            "kg. Program execution halted.")
         Error("ERROR: Load limit exceeded!", " Load = " + stringValue(load) + _
            "kg for " + stringValue(counter) + strItem + " " + _
            stringValue(limit) + "kg limit exceeded!")
      Finally
         If load <= limit Then
            printValue("Load = " + stringValue(load) + _
               "kg for " + stringValue(counter) + strItem)
         End If
      End Try
      counter = counter + 1
   Loop Until counter > 10
End Rule

You can exit from a Try...Catch statement by using an Exit statement.

Note: See Exit Statement for more information.