In conjunction with xsl:catch, the xsl:try instruction allows recovery from dynamic errors occurring within the expression it wraps.

Category: instruction
Content: ( sequence-constructor , xsl:catch , ( xsl:catch | xsl:fallback )* )
Permitted parent elements: any XSLT element whose content model is sequence-constructor; any literal result element




The expression to be evaluated by the instruction may be defined either by a select attribute, or by an enclosed sequence constructor.



The value no is used to relax the requirement to recover result trees when failures occur in the course of evaluating the xsl:try instruction. The default is yes.

Notes on the Saxon implementation

Note that within an xsl:try instruction, disable-output-escaping has no effect. (According to the specification, "when writing to a final result tree that is not being serialized, the request to disable output escaping is ignored.")


It is possible to have more than one xsl:catch within an xsl:try; the first one that matches the error is used.

Within the xsl:catch, a number of variables are available in the namespace http://www.w3.org/2005/xqt-errors:

The error can be re-thrown by using the error() function.


Example 1

The following example shows how to recover from an error in evaluating an XPath expression (in this case, divide-by-zero):

<xsl:try select="salary div length-of-service"> <xsl:catch errors="err:FAOR0001" select="()"/> </xsl:try>

Example 2

The following example shows how to recover from an error in evaluating a sequence of XSLT instructions (in this case, a validation error):

<xsl:try> <xsl:copy-of select="$result" validation="strict"/> <xsl:catch> <xsl:message>Warning: validation of result document failed: Error code: <xsl:value-of select="$err:code"/> Reason: <xsl:value-of select="$err:description"/> </xsl:message> <xsl:sequence select="$result"/> </xsl:catch> </xsl:try>

Links to W3C specifications

XSLT 3.0 Specification

See also