Exceptions

The language has support for try-catch style exception handlers. The syntax is (try A (catch B C)) where A is evaluated first and if exception is thrown, then C is evaluated with the symbol B bound to the value of the exception. Exceptions are thrown using the throw core function. You can give any data structure as argument to throw and it is passed along to catch. This way exceptions can contain more data than just a string that represents an error message.

Simple example of throwing and catching an exception:

> (try (throw {"msg":"message"}) (catch ex (str "error: " (get ex "msg"))))
"error: message"

Exceptions generated by PHP are catched as well. Their value will be a hash-map with keys type, file, line and message:

> (try (get "wrong" "key") (catch ex (get ex "type")))
"TypeError"

The REPL contains its own exception handler defined in PHP that will catch any exceptions thrown outside of try-catch form.