<try>

Exception handling processor

Core v2.2.0

Overview

The processor provides exception handling similar to Java try-catch. It executes its body and if any exception occurs, the block is executed with error details available in the ${_error} variable.

Usage Examples

Example 1: Handle HTTP errors gracefully

example-1.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<try>
  <def var="data">
    <http url="https://api.example.com/data"/>
  </def>
</try>
<catch>
  <file path="errors.log" action="append">
    HTTP request failed: ${_error}
    Timestamp: ${_date}
  </file>
  <def var="data" value=""/>  <!-- Use empty default -->
</catch>
</config>

Example 2: Retry on failure

example-2.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="success" value="false"/>
<def var="retries" value="0"/>

<while condition="${!success && retries < 3}">
  <try>
    <http url="https://api.example.com/data"/>
    <def var="success" value="true"/>
  </try>
  <catch>
    <def var="retries">
      <script>parseInt(context.getVar("retries")) + 1</script>
    </def>
    <sleep milliseconds="2000"/>
  </catch>
</while>
</config>

Example 3: Continue processing on XPath failure

example-3.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<loop item="product">
  <xpath expression="//product">...</xpath>
  
  <try>
    <def var="price">
      <xpath expression=".//price/text()">${product}</xpath>
    </def>
  </try>
  <catch>
    <def var="price" value="N/A"/>  <!-- Default if XPath fails -->
  </catch>
  
  <file path="products.txt" action="append">
    Product price: ${price}
  </file>
</loop>
</config>

Important Notes

Related Processors