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

Curly braces are required on the catch expression if any of the following are true.

  1. you specify a custom exception variable (as of 0.7)
  2. it starts with a variable name
  3. it is more than one statement
  4. you want to use an else section

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.

As of 0.7, you can also use else if on catch.

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 an exception variable is not specified, it is implicitly _err. Prior to 0.7, the default exception variable was .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 .e { ... } }

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.