Try-catch expressions

JSONiq follows the W3C standard for try-catch expressions. The following explanations, provided as an informal summary for convenience, are non-normative.

Figure 52. 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 111. 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 112. 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 113. A try catch expression

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

Result (run with Zorba): syntax error