Product SiteDocumentation Site

5.5.3. Try-catch expressions

Figure 5.35. TryCatchExpr

A try catch expression evaluates the expression inside the try block and returns its resulting value.
However, if an error is raised dynamically, the catch clause is evaluated and its result value returned.
Example 5.98. A try catch expression
try { 1 div 0 } catch * { "division by zero!" } 
Result (run with Zorba):
division by zero!

Only errors raised within the lexical scope of the try block are caught.
Example 5.99. A try catch expression
let $x := 1 div 0
return try { $x }
       catch * { "division by zero!" } 
Result (run with Zorba):
An error was raised: division by zero

Errors that are detected statically within the try block are still reported statically.
Example 5.100. A try catch expression
try { x } catch * { "syntax error" } 
Result (run with Zorba):
syntax error