A very simple exception handling system has been added to Brat. Like most features, though, this does not add any new syntax, just some new functions you can use.
protect executes a block and will catch any exceptions thrown inside of it. Exceptions are just Brat objects with three particular fields:
stack_trace. You can throw or raise an exception using
throw, which takes either a string or an exception.
The simplest thing to do is to just swallow all exceptions and carry on merrily:
The code above will completely ignore the exception. This is typically not what you want, so
protect takes a
rescue option. Using the latest syntax for implicit hash parameters, it looks like this:
You can create exceptions using just strings, but there a few convenience methods for you. Right now, you can use
convert_error. These will generate consistent error messages from a few parameters.
Here’s an example:
That is all there is, right now. However, there is clearly room for additions, such as filtering on specific exception types or adding an
ensure clause. Ideas for the future!
Most of Brat (all except the standard libraries, I believe) will throw proper exceptions. Exceptions from Neko code will get wrapped in an exception if they are caught via