The Try-Catch Expression is another welcome addition to the XML Query Language. This page introduces the expression and provides some examples.
While the first version of the xquery language provided the possibility to raise errors, the language had no standard mechanism to handle these errors. The following two queries show how it works:
"Could not locate collection: /db/not-existent/"
Additional information about the error can be obtained from two variables which are set by the function:
For more details check the eXist-db function documentation.
In version 3.0 of XQuery language the handling of errors has become part of the language: the try-catch expression. An introduction example:
The "catch error list" specifies wich errors are handled in the next catch expression; the asterix wildcard is an special example meaning all errors match.
It is possible to have more catch clauses, the values in the catch-error-list determines which catch block is executed.
The following query is a nice complete example of the try-catch expression. The outcome of the query is
The java exception of the second example can be handled as well:
The expression is available starting eXist-db 1.5 ; in Jan 2012 changes were made to reflect the latest specification