Exception handling uses the familiar try / catch
, try / catch / finally
and try / finally
constructions. Their semantics are the same as found in other languages such as Java, especially
regarding the handling of finally
blocks.
The following snippets show each exception handling form.
# Good old try / catch try { something() } catch (e) { e: printStackTrace() } # A try / finally try { doSomething() } finally { cleanup() } # Full try / catch / finally construct try { doSomething() } catch (e) { e: printStackTrace() case { when e oftype IOException.class { println("Oh, an I/O exception that I was expecting!") } when e oftype SecurityException.class { println("Damn, I didn't expect a security problem...") throw e } otherwise { throw e } } } finally { cleanup() }
Because Golo is a weakly typed dynamic language, you need to check for the exception type
with the oftype
operator. In a statically typed language like Java, you would instead have several
catch
clauses with the exception reference given a specific type. We suggest that you take
advantage of the case
branching statement.