Return exressions are written using RETURN(tree)
:
RETURN(LIT(0)) // return 0
Exceptions are thrown using THROW(tree)
or THROW(sym|c, [tree|"message"])
:
THROW(REF("x"))
THROW(IllegalArgumentExceptionClass)
THROW(IllegalArgumentExceptionClass, "oh no")
THROW(IllegalArgumentExceptionClass, REF("x"))
These examples print as:
throw x
throw new IllegalArgumentException()
throw new IllegalArgumentException("oh no")
throw new IllegalArgumentException(x.toString)
Exceptions are caught using TRY(stat, ...) CATCH(CASE(pat), ...) [ENDTRY|FINALLY(...)]
. CASE(...)
accepts a pattern matching expression, which is shown briefly here, and covered in detail later:
(TRY (REF("something") APPLY LIT(0))
CATCH (
CASE(WILDCARD) ==> (Predef_println APPLY LIT("error"))
) ENDTRY)
(TRY (REF("something") APPLY LIT(0))
CATCH (
CASE(WILDCARD) ==> (Predef_println APPLY LIT("error"))
) FINALLY(Predef_println APPLY LIT("finally")))
In the above examples, WILDCARD
is a pattern expression that matches to anything. The examples print as:
try {
something(0)
} catch {
case _ => println("error")
}
try {
something(0)
} catch {
case _ => println("error")
} finally println("finally")