General form:
try(
...some code...
) catch(Error,
...some code...
) catch(Exception,
...some code...
)
Note that, again, no special syntax is required. try and catch are methods. As such, it is not necessary for try to immediately be followed by a catch:
e := try(foo bar) # foo and bar don't exist writeln(e) #=> # Exception_0x8093b0: # coroutine = Coroutine_0x8074a8 # error = "Object does not respond to 'foo'"
If an error occurs, try returns the exception raised (usually an Exception or Error object or a descendant thereof). If all the code inside try works without errors, it returns nil. (For this reason, nil has catch and pass methods that have no effect.)
Naturally, it's possible to define your own exceptions. For example, by cloning Exception.
Exceptions have a raise method:
ScroogeException := Exception clone
foo := method(
ScroogeException raise("Bah humbug")
)
e := try(foo)
writeln(e)
#=>
# ScroogeException_0x80e670:
# coroutine = Coroutine_0x80d430
# error = "Bah humbug"
To re-raise a caught exception, use the pass method:
e := try(foo) e pass
The manual points out this useful idiom:
e := try(
...
) catch(AnException,
...
) catch(AnotherException,
...
) pass
...which re-raises the exception if all catch clauses fail.
IMPORTANT: If pass is not specified, the error is not automatically re-raised, but not handled by a catch either! This is because try does the actual catching, while catch does the handling. (Compare e.g. Python where an exception isn't caught if it's not handled by an except clause.)