Wiki

Changes between Initial Version and Version 1 of Throw

Show
Ignore:
Timestamp:
03/23/10 11:24:59 (14 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Throw

    v1 v1  
     1= Throw = 
     2 
     3The '''throw''' statement is used to signal the occurrence of an anomalous situation (exception) during the program execution. 
     4 
     5The thrown exception is an object whose class is derived from System.Exception, for example: 
     6{{{ 
     7class MyException inherits System.Exception 
     8    pass 
     9# ... 
     10throw MyException() 
     11}}} 
     12 
     13Usually the throw statement is used with try-catch-finally statements. 
     14 
     15You can also rethrow a caught exception using the throw statement by specifying the caught exception 
     16{{{ 
     17catch exc as MyException 
     18    if passOn 
     19        throw exc  # rethrow caught exception 
     20    # ... 
     21}}} 
     22 
     23== Grammar == 
     24{{{ 
     25throw EXCEPTION_OBJECT 
     26}}} 
     27 
     28== Example == 
     29{{{ 
     30class ThrowTest 
     31    def getNumber(index as int) as int 
     32        nums = [ 300, 600, 900 ] 
     33        if index > nums.length 
     34            throw IndexOutOfRangeException() 
     35        return nums[index] 
     36 
     37    def main is shared  
     38        result = ThrowTest.getNumber(3) 
     39 
     40/# 
     41    Output: 
     42    The System.IndexOutOfRangeException exception occurs. 
     43#/ 
     44}}}