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] { ... }

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.

Catch has 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 } } # 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.

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 be written as follows, 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.