Skip to content
David Jeske edited this page May 23, 2017 · 7 revisions

Irken's exceptions are polymorphic variants, and the current syntax is derived from Python instead of Scheme.

   (try
       < body thunk >
    except
       ( <exception pattern> ) -> <exception handler code>
       ...
     )

One interesting gotcha about catching exceptions is that because try/except is an expression, it must always return the same type of value. That means every one of the < exception handler code > expressions must evaluate to the same type of value as the < body thunk > -- even if the expression result type is never used.

Should we have a different form, such as try! which is a statement only, ignoring the results of it's contained expressions, and always returning #u? This might make for better quality of life in some code.

For example, in the code below try block evaluates to an int, so we put a "2" at the end of our exception block to also return an int. If an exception handler re-raises, then the expression never exits and it isn't required to have a matching expression evaluation type.

(include "lib/basis.scm")

(define (a)
   (raise (:mySillyException "yo"))
   1)

 (try
     (a)  ; results in an int
  except
     (:mySillyException e) ->
        (begin (printf "caught exception: " e "\n")
               2 ;; we have to result in the same type as the try expression
        )
  )

Next: Tail Recursion


Further work on exceptions