The Try Statement

The Try statement provides a mechanism for catching exceptions that occur during execution of a nested statements block. The Try statement also enables the user to specify a block of statements that are always executed when control leaves the Try statement.

try-statement:

Try   block   catch-clauses   End Try

Try   block   finally-clause   End Try

Try   block   catch-clauses   finally-clause   End Try

catch-clauses:

Except   specific-catch-clauses   else-catch-clauseopt

Except   block

specific-catch-clauses:

specific-catch-clause

specific-catch-clauses   specific-catch-clause

specific-catch-clause:

On   exception-variable-declarationopt   class-type   Do

Beginopt   block   Endopt;

exception-variable-declaration:

identifier   :

else-catch-clause:

Else

Beginopt   block   Endopt;

finally-clause:

Finally   block

There are three possible forms of the Try statement:

If a handler type is specified for the Except block, this type must derive from System.Exception.

If both the handler type and identifier are specified in the Except block, an exception variable of the given name and type is declared. The exception variable is similar to a local variable with a scope that includes the statement block in the Except block. During the handler execution, the exception variable represents the exception currently being handled.

The Except handler without the specified handler type is named a general purpose handler. The Try statement may contain only one general purpose handler, and this handler is to be the last in the exception handler chain.

Example

Imports System;
Public Class TestException
    Public Sub Test(a, b: integer);
    Begin
        //Procedure code
        //Test ratio of parameters A and B
        If <Test condition> Then
            Raise New ArgumentException("Invalid ratio of parameters A and B");
        //Testing valid range of parameter values
        Elseif <Test condition> Then
            Raise New ArgumentOutOfRangeException("Parameter values are outside the valid range");
        Else
            //Main procedure code executed if
            //all parameters tests are passed
        End If;
    End Sub Test;
End Class TestException;

Public Class Program
    Public Sub Main();
    Var
        Obj: TestException = New TestException();
    Begin
        Try
            //Execute the Test procedure with some parameter values
            Obj.Test(<Parameter values>);
        Except On Ex: ArgumentException Do
            Begin
                //Handle exception
                //when ratio of parameters A and B is invalid
            End;
        On Ex: ArgumentOutOfRangeException Do
            Begin
                //Handle exception
                //when values of parameters A and B lie outside the valid range
            End;
        Else
            Begin
                //Handle all other exceptions 
            End;
        Finally
            //Always executable block
        End Try;
    End Sub Main;
End Class Program;

See also:

Statements