<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="xsl-elements" page="try" subpage=""/>
      <!--
           Generated at 2011-12-09T20:47:22.916Z--><title>Saxonica: XSLT and XQuery Processing: xsl:try</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title" content="Saxonica: XSLT and XQuery Processing: xsl:try"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>xsl:try</h1>
      <p>The <code>xsl:try</code> instruction is new in XSLT 3.0: in conjunction with <code>xsl:catch</code> it allows
         recovery from dynamic errors.</p>
      <p>The following example shows how to recover from an error in evaluating an XPath expression (in this case, divide-by-zero):</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>
&lt;xsl:try select="salary div length-of-service"&gt;
  &lt;xsl:catch errors="err:FAOR0001" select="()"/&gt;
  &lt;/xsl:try&gt;</code>
         </pre>
      </div>
      <p>The following example shows how to recover from an error in evaluating a sequence of XSLT instructions (in
               this case, a validation error):</p>
      <div class="codeblock"
           style="border: solid thin; background-color: #B1CCC7; padding: 2px">
         <pre>
            <code>
&lt;xsl:try&gt;
    &lt;xsl:copy-of select="$result" validation="strict"/&gt;
    &lt;xsl:catch&gt;
      &lt;xsl:message&gt;Warning: validation of result document failed:
          Error code: &lt;xsl:value-of select="$err:code"/&gt;
          Reason: &lt;xsl:value-of select="$err:description"/&gt;
      &lt;/xsl:message&gt;
      &lt;xsl:sequence select="$result"/&gt;
    &lt;/xsl:catch&gt;
&lt;/xsl:try&gt;               
</code>
         </pre>
      </div>
      <p>The <code>errors</code> attribute of <code>xsl:catch</code> indicates which error codes are caught. If absent, or it set to
         <code>*</code>, all errors are caught. The value can be a whitespace-separated list of QNames; the wildcards <code>*:local</code>
         or <code>prefix:*</code> can also be used.</p>
      <p>It is possible to have more than one <code>xsl:catch</code> within an <code>xsl:try</code>; the first one that matches the error
         is used.</p>
      <p>Within the <code>xsl:catch</code>, a number of variables are available in the namespace
               <code>http://www.w3.org/2005/xqt-errors</code>:</p>
      <ul>
         <li>
            <p>err:code - the error code as a QName</p>
         </li>
         <li>
            <p>err:description - the error description (error message)</p>
         </li>
         <li>
            <p>err:value - the error object (if available)</p>
         </li>
         <li>
            <p>err:module - the URI of the stylesheet module in which the error occurred</p>
         </li>
         <li>
            <p>err:line-number - the line-number of the source stylesheet where the error occurred</p>
         </li>
         <li>
            <p>err:column-number - for Saxon this will generally be unknown (-1)</p>
         </li>
      </ul>
      <p>The error can be re-thrown by using the <code>error()</code> function.</p>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="value-of.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>