Try...Catch Loop
Language Items List
Definition:
Defines a block of statements that is capable of handling exceptions that
occur during the execution of those statements.
Syntax:
Try
[Try-statements]
Catch <exceptionname-1>[(args]]
[Catch-statements]]
[Throw]
[Catch <exceptionname-n>[(args)]
[Catch-statements]]
[Throw]
[Catch
[Catch-statements]]
[Throw]
End Try
Syntax Description
Try Marks the beginning of the Try...Catch block.
try-statements The statement(s) to execute.
Catch Marks the beginning of a Catch block for the previous Try block.
Exceptionname-1 Specifies the name of the exception to catch.
Args Specifies the arguments that the specified exception provides.
catch-statements Specifies the statements to execute if the exception name specified by the
Catch is caught.
Exceptionname-n Specifies the next exception to catch.
Catch Catches all exceptions (when no "exception-name" is provided).
Throw Re-throws current exception (when no "exception-name" is provided).
End Try Marks the end of the Try...Catch block.
Details:
To handle any error in a block of code, the desired code is placed in a Try...
block (Try ... End Try) that contains one or more Catch blocks (Catch
exceptionname ... ). If something goes wrong, a named exception is thrown. If an
exception is thrown, the matching catch block will be invoked immediately and the
code within that catch block is run, thus allowing for maximum flexibility.
Try...Catch loops can be used to:
Catch specific exceptions and deal with them by providing arguments that
specify more about the exception.
Catch all exceptions and indicate that clean-up is necessary by specifying a
Catch clause that does not use an "exception-name."
Catch and kill all exceptions beyond a certain point by specifying that
regardless of what happens after a certain procedure, no exceptions will be passed
on. This essentially "bullet-proofs" a procedure.
The statements specified by "Try-statements" are executed just like any other
statement block. The difference is that if an exception occurs during their
execution, the exception is compared against the exceptions that are to be
handled, as indicated by the Catch clauses. If the exception matches one of the
specified exceptions, the "Catch-statements" block will be executed.
The code that is run in a Catch block, can perform a number of operations
specific to whatever error handling is necessary for the particular exception that
is caught. The Catch block can then choose to either re-throw the original
exception by using "Throw" (without an exception name), or to throw a different
exception that a Try..Catch block prior to the current one can handle (no other
catches will be activated for this particular Try..Catch block, except in the
case of a recursively called procedure). The Catch block can also choose to
consider the exception handled by not doing anything. In this case, execution will
continue with the next statement after the Try..Catch block (i.e. the statement
following the last Catch clause.)
Specifying a Catch clause that does not use an "exception-name" will have the
effect of handling all exceptions. This type of catch is usually placed after
all other catches, since it renders Catches defined below it useless. This type
of Catch is useful for indicating that the attempted operation failed (for
whatever reason), and that some cleanup is necessary.
Providing for the clean-up of allocated resources in the face of exceptions is
good programming practice. In the following example, a procedure is called
that could possibly throw. The "catch all" style Catch block is used to protect
the resource (the MousePointer) from having a bad value after the procedure is
finished.
This example shows how a Try..Catch block can be used to make sure the
MousePointer property is properly reset, even if the call to "do_long_operation"
throws an exception. If an exception is thrown, the handler specified by the empty
catch clause will catch all exceptions, reset the MousePointer, then cause the
original exception to be re-thrown.
Sub ExceptionTest()
Try
MousePointer = "Hourglass"
do_long_operation()
Catch
MousePointer = "Default"
Throw
End Try
MousePointer = "Default"
End Sub
The catch-all phrasing can also be used to make a particular procedure
"bullet-proof." A bullet-proof procedure specifies that regardless of what happens
after a particular procedure, no exceptions will be passed on.
The following example shows a bullet-proof Catch clause; regardless of what
happens in DangerousProcedure( ), the call to BulletProof( ) will always succeed.
Sub BulletProof()
Try
DangerousProcedure()
Catch
End Try
End Sub
See Also:
Throw Statement