The Try statement provides a mechanism for catching exceptions that occur during execution of an embedded 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:
A Try statements block followed by one or more Except statements blocks.
A Try statements block followed by a Finally statements block.
A Try statements block followed by one or several Except statements blocks followed by a Finally statements block.
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 operator 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.
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("Unacceptable ratio of parameters A and B");
//Testing acceptable range of parameter values
Elseif <Test condition> Then
Raise New ArgumentOutOfRangeException("Parameter values are outside the acceptable 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
//Running the procedure Test with some parameter values
Obj.Test(<Parameter values>);
Except On Ex: ArgumentException Do
Begin
//Handling an exception
//when the ratio of parameters A and B is unacceptable
End;
On Ex: ArgumentOutOfRangeException Do
Begin
//Handling an exception
//when values of parameters A and B lie outside the valid range
End;
Else
Begin
//Handling other exceptions
End;
Finally
//Always executable block
End Try;
End Sub Main;
End Class Program;
See also: