THROW
Synopsis
THROW [oref]
Arguments
oref | Optional — A user-defined object reference. |
Description
The THROW statement explicitly issues an exception from within a block of code defined by a TRY statement. Issuing a THROW transfers execution from the TRY block to the corresponding CATCH exception handler.
THROW is used to issue an explicit exception. MVBasic issues an implicit exception when a runtime exception occurs. A runtime exception generates an exception object which it throws to a CATCH exception handler.
THROW has two forms:
-
Without an argument
-
With an argument
THROW without an Argument
Argumentless THROW transfers exception processing to the corresponding CATCH exception handler. No object is pushed on the stack, but the %New() method is called.
THROW with an Argument
THROW oref specifies a user-defined object reference, which it throws to the CATCH statement.
Arguments
expression
A user-defined object reference (oref). For example, THROW "Sample.MyException"->%New("Example Error",45). The creation and population of this exception object is the responsibility of the programmer.
Examples
The following example shows the use of THROW:
TRY
PRINT "about to issue a THROW statement"
THROW "Sample.MyException"->%New("Example Error",45,"Sample Program")
PRINT "this should not display"
CATCH myvar
PRINT "this is the exception handler"
PRINT :myvar->Name,"Error Name"
PRINT :myvar->Code,"Error Code Number"
PRINT :myvar->Location,"Error Location"
END TRY
PRINT "this is where the code falls through"