This is an old revision of the document!
Like in C++, the
catch keywords are used to begin compounds of code where exceptions can be thrown and processed.
If an exception is thrown outside
catch, it will be uncaught and execution of the script to be aborted.
try: print 'Nothing thrown yet.' catch: print 'Skipped.'
Exceptions are thrown using a
throwcan be used inside a
catchcompound to rethrow the caught exception.
try throw "There was an error" catch print 'Exception caught' end
If the exception does not require any handling, one can use
pass as the catch compound.
try: throw 'Problem!' catch: pass
The exception message can be assigned to a local variable so it can be accessed in the catch compound.
try: throw "OMG!" catch Error, er print 'Caught:', er end
Caught: [Error] (in script) OMG!
Try/catch can be nested, and an exception thrown in an inner try compound can be caught in an outer compound.
try try try throw 'Deep.' catch BogusError end catch BogusError 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 compounds are skipped. The output is:
Got it: [Error] (in script) Deep.