Throw Statement
Language Items List
Definition:
Specifies that a named exception should be generated and thrown from the
current execution point.
Syntax:
Throw [<exceptionname>[(args)]]
Syntax Description
Throw Keyword used to generate exceptions.
exceptionname Name of exception to throw.
args Optional arguments that the named exception includes.
Details:
The Throw statement is used by program code to signal errors when the program
has determined that something has gone wrong and execution cannot continue from
that particular point. For example, assume that a file read operation must
first open a file before reading. If a file cannot be opened, the procedure would
"throw" an exception. Simply continuing at this point, would cause problems.
Similarly, a return value cannot always be used to indicate a failure. For
example, using the file read example but requiring the operation to return any valid
integer instead, would create a situation where there is no value that could be
used to signal an error.
The other standard method used to signal errors is to set an object property
that can be checked after calling a function. This method, however, burdens the
caller with a lot of error checking responsibilities. The recommended method
for dealing with unexpected errors is to throw exceptions, which forces the
exceptional condition to be dealt with by qualified error handlers.
There is a difference between exceptions that the system generates and ones
that interpretive code generates with the Throw statement. This distinction
allows the integrated debugger to trap the exceptions independently and suspend on
throws of system exceptions, but not on throws of interpretive exceptions.
To "rethrow" an exception that has been caught in a text block, use the Throw
statement without arguments. Doing this causes the same exception to roll back
through the code (through each stack frame) until it finds the next matching
handler for the exception. If no matching handler is found, the default handler
is triggered.
See Also:
Try...Catch Statements
Example:
The Sub "Test" illustrates using an Interpretive Try..Catch block to guard
against certain interpretive exceptions, namely the FileNotFound exception. When
this exception occurs as a result of executing the "GetInteger" function the
program will respond by posting an InfoBox.
The Function "GetInteger" shows how one exception (e.g. a system exception)
can be turned into an interpretive exception. This is done with a Try..Catch
block that catches the "FileError" exception and responds by throwing the
"FileNotFound" exception. This is done to generate a custom exception that has more
information, like the filename in this example.
Sub Test()
Try
Dim result as Integer
result = GetInteger()
Catch FileNotFound(fn As String)
InfoBox "File could not be opened: " & fn
End Try
End Sub
Function GetInteger() As Integer
Try
TextFile.FileName = "demo.txt"
TextFile.Open(True)
GetInteger = 99
Catch FileError
Throw FileNotFound("demo.txt")
End Function