langur

langur exceptions

An exception in langur is always a hash that is guaranteed to contain certain fields, even if they are empty. You may include other fields in a hash that you throw. You can also throw anything else and it becomes the required hash.

If you do not specify a source in what you throw, langur sometimes determines and adds the function name automatically.

exception hash

Any exception hash is guaranteed to contain the following fields.

"cat" an exception general category string
"msg" the message string
"src" exception source string
"hst" previous exception or null

catching exceptions

... catch [.err] { ... } [ else { ... } ]

There is no explicit try block. All the statements preceding a catch within a block are the implicit try block. You could put a catch at the end of a script to cover the entire script.

If a catch is embedded within a catch, they may each use the same exception variable name as they each have their own scope.

If you do not rethrow a caught exception, it is not propogated past the catch.

An else section is optional. It will only be run if there is no exception. This was added to langur as of version 0.5.0.

Catch and else have scope. Since try does not have scope, variables declared within the implicit try section but still in scope after the catch will be the default value (null) if they are not initialized before an exception.

catch exception example

123 / 0 catch .err { if .err["cat"] == "math" { 456 } else { throw .err } } else { # no exception 890 } # result == 456

simplified catch

If a single expression is used and does not start with a variable name, curly braces are not required on the catch expression.

A catch not using block syntax (curly braces) cannot have an else section.

If an exception variable is not specified, it is implicitly .err.

Using throw within a catch expression does not require you to specify what to throw. It implicitly rethrows the caught exception.

The example above could almost be written as follows (leaving out an else section), using the implicit .err variable, leaving out curly braces, and implicity rethrowing the exception caught.

123 / 0 catch if .err["cat"] == "math" { 456 } else { throw }

still want try?

You can emulate a try block by wrapping into a scoped block.

{ ... catch .err { ... } }

Go panics

While a panic (a Go language exception) may indicate a bug in langur, we decided it best to try catching them and converting them to langur exceptions. This still does not work with out-of-memory (OOM) errors. As a concise explanation, someone wrote "All OOM conditions terminate a Go program."

When a Go panic is caught and converted to a langur exception, the source name starts with "panic:" and may or may not have more information.