Exceptions and errors

Kawa supports the exception framework and forms from R6RS and R7RS. See Exceptions for details.

Native exception handling

You can also work with native Java exceptions at a low level.

The primitive-throw procedure throws a Throwable value. It is implemented just like Java’s throw.

(primitive-throw (java.lang.IndexOutOfBoundsException "bad index"))

You can catch an exception with the try-catch syntax. For example:

(try-catch
  (do-a-bunch-of-stuff)
  (ex java.lang.Throwable
    (format #f "caught ~a~%~!" ex)
    (exit)))

A try-finally does the obvious:

(define (call-with-port port proc)
  (try-finally
   (proc port)
   (close-port port)))

Both try-catch and try-finally are expression forms that can return values, while the corresponding Java forms are statements that cannot return values.