Version 2 (modified by todd.a, 14 years ago) |
---|
Throw
The throw statement is used to signal the occurrence of an anomalous situation (exception) during the program execution.
The thrown exception is an object whose class is derived from System.Exception, for example:
class MyException inherits System.Exception pass # ... throw MyException()
Usually the throw statement is used with try-catch-finally statements.
You can also rethrow a caught exception using the throw statement by specifying the caught exception
catch exc as MyException if passOn throw exc # rethrow caught exception # ...
Grammar
throw EXCEPTION_OBJECT
Example
class ThrowTest def getNumber(index as int) as int nums = [ 300, 600, 900 ] if index > nums.length throw IndexOutOfRangeException() return nums[index] def main is shared result = ThrowTest.getNumber(3) /# Output: The System.IndexOutOfRangeException exception occurs. #/