since version 2.1.0
This macro will execute its input if there was an error caught by a try
macro.
The macro catch
can be used to handle errors in a macro file.
{@try {@include} file name is missing}{@catch there was an error}
will result in
there was an error
The general rule is that you should use the catch
macro in the same scope where the try
macro is used.
However, it can also be used a bit more complex than that.
Here are the detailed rules:
-
A
catch
executes the content if there was an error executing the lasttry
on the same level as the catch, and it was not caught yet by anothercatch
on the same level. -
A
catch
executes the content is there was an error executing atry
on a higher level, and it was not caught by anothercatch
on the same or higher level as thecatch
is and there was no non-failingtry
. -
A
catch
executing clears the error on the level where thecatch
is. -
Errors are automatically cleared when a scope closes.
These rules may seem complex, but it is easy to understand if you know how the error handling is implemented.
The error handling is implemented using the option try$caught$error
.
When an error is detected by a try
macro, this option is set.
If there is no error, then the option is reset, hence clearing any error on the same level.
When catch
is executed, it checks the value of the option and resets it.
When executing in a nested scope, the reset will not clear the error on the higher level.
The name of the option was chosen to be user-friendly so that you can
-
export it to higher scopes,
-
query its value, presumably in an
if
macro, -
set and reset it using the
options
macro.
Here is a simple example that uses this option by the name directly:
{@try {@include}}{#if/{try$caught$error}/there was an error}
{#if/{try$caught$error}/still there is an error, not cleared}{@catch}
{#if/{try$caught$error}/no, this one will not be displayed/error cleared}
will result in
there was an error
still there is an error, not cleared
error cleared