This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
script:walkthrough:exceptions [2019-11-20 19:16] – [Try and catch] skyjake | script:walkthrough:exceptions [2019-11-20 19:29] (current) – [Catching native errors] skyjake | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <- Statements and compounds ^ ^ Records -> | ||
+ | ====== Exceptions ====== | ||
+ | |||
+ | ===== Try and catch ===== | ||
+ | |||
+ | Like in C++, the '' | ||
+ | |||
+ | If an exception is thrown outside '' | ||
+ | |||
+ | try: print ' | ||
+ | catch: print ' | ||
+ | |||
+ | ===== Throwing exceptions ===== | ||
+ | |||
+ | Exceptions are thrown using a '' | ||
+ | * When given one argument, a new exception is thrown with the argument converted to a text string. The argument should be used for instance for error messages. | ||
+ | * Without arguments, '' | ||
+ | |||
+ | try | ||
+ | throw "There was an error" | ||
+ | catch | ||
+ | print ' | ||
+ | end | ||
+ | |||
+ | ===== Handling exceptions ===== | ||
+ | |||
+ | If the exception does not require any handling, one can use '' | ||
+ | |||
+ | try: throw ' | ||
+ | catch: pass | ||
+ | |||
+ | The exception message can be assigned to a local variable so it can be accessed in the catch block. | ||
+ | |||
+ | try: throw " | ||
+ | catch Error, er | ||
+ | print ' | ||
+ | end | ||
+ | |||
+ | Output: | ||
+ | |||
+ | Caught: [Error] (in script) OMG! | ||
+ | |||
+ | |||
+ | Multiple '' | ||
+ | |||
+ | try | ||
+ | throw 'This will be printed' | ||
+ | | ||
+ | catch NullError: print 'Got NullError' | ||
+ | catch Error, er: print er | ||
+ | catch: print 'Never here.' | ||
+ | |||
+ | Try/catch can be nested, and an exception thrown in an inner try block can be caught in an outer block. | ||
+ | |||
+ | try | ||
+ | try | ||
+ | try | ||
+ | throw ' | ||
+ | catch BogusError | ||
+ | end | ||
+ | catch BogusError | ||
+ | end | ||
+ | try | ||
+ | catch Error, er | ||
+ | print ' | ||
+ | end | ||
+ | catch Error, er | ||
+ | print 'Got it:', er | ||
+ | end | ||
+ | |||
+ | In this example, BogusError does not match the type of error thrown in scripts (a generic Error), so the catch blocks are skipped. The output is: | ||
+ | |||
+ | Got it: [Error] (in script) Deep. | ||
+ | |||
+ | An exception thrown in a function unwinds the call stack to find a matching catch block. | ||
+ | |||
+ | def erroneous() | ||
+ | throw " | ||
+ | end | ||
+ | | ||
+ | try | ||
+ | print " | ||
+ | erroneous() | ||
+ | try | ||
+ | print 'This will be skipped!' | ||
+ | catch | ||
+ | print 'I am on the wrong catch level.' | ||
+ | end | ||
+ | catch | ||
+ | print ' | ||
+ | end | ||
+ | |||
+ | ===== Catching native errors ===== | ||
+ | |||
+ | Native code may throw various kinds of exceptions that can then be caught in the script. In the example below, a '' | ||
+ | |||
+ | try | ||
+ | record temp | ||
+ | reference = temp | ||
+ | del temp | ||
+ | print reference | ||
+ | catch NullError, er | ||
+ | print 'Oh noes:', | ||
+ | end | ||
+ | |||
+ | '' | ||
+ | |||
+ | const SOME_CONSTANT = 3.1415 | ||
+ | try | ||
+ | SOME_CONSTANT = 3 | ||
+ | catch ReadOnlyError, | ||
+ | print ' | ||
+ | end | ||
+ | try | ||
+ | const SOME_CONSTANT = 3 | ||
+ | catch ReadOnlyError, | ||
+ | print 'Will not work either:', | ||
+ | end | ||
+ | |||
+ | Any type of native exception can be caught in a script. |