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:

  (ex java.lang.Throwable
    (format #f "caught ~a~%~!" ex)

A try-finally does the obvious:

(define (call-with-port port proc)
   (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.