Try-catch expressions

Figure 41. 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 105. 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 106. 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 107. A try catch expression

try { x } catch * { "syntax error" } 

Result (run with Zorba): syntax error