try. Intercepting and handling errors

^try{the code whose errors get…}{…into this handler as $exception}
^try{the code whose errors get…}{…into this handler as $exception}{the code which will be executed anyway}
   [3.3.0]

If an error occurred during processing the
code, a variable $exception will be created and control over processing will be handed over to handler.
If third parameter was specified, that code will be executed anyway regardless of unhandled exception.

$exception is such a hash:

$exception.type
string, error type.
There is a number of system error types; a type can also be defined in operator throw.
$exception.source
string, error source (wrong filename, method's name, …)
$exception.file
$exception.lineno
$exception.colno
file containing source, line and column numbers in it
$exception.comment
error comment, in English
$exception.handled
true or false, flag "if error has been handled"
you will need to set the flag in the handler if you have handled the received error


Handler must report Parser if the error has been handled. For this purpose, it must set the flag but only for the needed error types:
$exception.handled(true)

If
handler has not set the flag, the error is considered unhandled and will be handed over to another handler, if it exists.

If the error remains unhandled, method
unhandled_exception is called. This method will receive information on the error as well as the stack of calls that caused it. The method's work will result in a custom message to be output to a visitor. The error will also be recorded in server's error log.

Example

^try{
   $srcDoc[^
xdoc::create{$untrustedXML}]
}{
   ^if($exception.type 
eq xml){
      $exception.handled(
true)
      Invalid XML,
      
<pre>$exception.comment</pre>
   }
}



Copyright © 1997–2021 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 24.06.2012