Try…Catch…Finally Statement
Lets you catch many errors that can occur in a given block of code.
Syntax
Try
[ try_statements ]
[ Catch [ exception [ As type ] ]
[ catch_statements ] ]
…
[ Finally
[ finally_statements ] ]
End [ Try ]
Parts
try_statements- Optional statements in which errors can occur.
Catch- Optional start of a block of statements that you can use again and again. It runs when an error occurs.
exception- Optional name of an object variable.
- If not given,
Catchwill run for all errors. This can be done only as the last block in a sequence of such blocks. type- Optional data type of the error object.
The type of the object given to
Raiseselects which block (Catch) runs. If not given, the default isError. catch_statements- Optional statements that run when an error occurs.
finally_statements- Optional statements that always run.
If an error occurred, the statements run after
Catchis done. End- Completes the statement.
You can also use
End Try. - You can change this part of the syntax. See @Option Directive for more information.
Instructions
An exception is an error that can occur at run-time.
If you think a section of code can cause an exception, put the code in a block that starts with Try.
Then use a block that starts with Catch to keep control if the exception occurs.
The block that starts with Try can have one or more clauses that start with Catch that follow it.
Each Catch is a handler for each different exception.
When code raises an exception, ViviFire tries to find the handler that agrees with the given exception.
If ViviFire cannot find a handler in this scope, it moves control back to the procedure that called this one.
It continues up the call stack until it finds a handler for the given exception.
But if it cannot find a handler, ViviFire shows an error message to the user and stops the program.
You can use more than one Catch as part of the statement Try…Catch…Finally.
If you do this, the sequence of the clauses has an effect because ViviFire examines them in sequence.
Catch the more special exceptions before the more general exceptions.
The most general conditions for Catch that follow will catch all exceptions that inherit from the class Error.
You typically use one of these constructs as the last in the sequence of clauses.
Control flow cannot move to a clause that follows one of these constructs.
Catch e As ErrorCatch
Local variables declared in Try are not available in Catch because each block is a different scope.
If you want to use a variable in more than one block, declare it before Try.
Finally block
If you have one or more statements that must run before you move out of Try, use Finally.
Control moves to this block immediately before it moves to the code after End Try.
This occurs if code raises an exception or runs correctly.
Although Return usually immediately moves control out of a procedure, the code in Finally runs Before the procedure returns.
The only conditions which do not let Finally run are as follows:
Exit PrograminTryorCatch-
StackOverflowErrorraised inTryorCatch
You cannot move out of Finally with the usual Control flow statements Exit and Return.
Only an exception can move control out of Finally.
If the statement Try contains Finally, blocks of Catch become optional.
Exception argument
TODO
Before you use Try…Catch…Finally
TODO
Iterators
TODO
Examples
Var x, y, z As Int32
Try
' Cause a division by zero.
x = y \ z
' This will not run.
PrintLine "End of Try"
Catch e As Error
' Show the error message.
PrintLine e.Message
Finally
PrintLine "End of Finally"
End Try
Division by zero End of Finally