Apache Ant – Error/Exception Handling

The Ant-Contrib library also provides <trycatch> task for error/exception handling. Here is an example of build.xml which demonstrate the usage and again it requires the ant-contrib-1.0b3.jar which i place it in the ant-lib folder.

<project name="ant-try-catch-example" default="run" basedir=".">

  <!-- Load the ant contrib lib -->
  <taskdef resource="net/sf/antcontrib/antcontrib.properties">
      <pathelement location="${basedir}/ant-lib/ant-contrib-1.0b3.jar"/>

  <target name="run">
    <trycatch property="error-prop" reference="error-ref">
          <istrue value="${specialError}"/>
            <fail message="Special error is thrown!"/>
            <fail message="General error is thrown!"/>

        <echo message="catched..."/>

        <echo message="finally"/>

    <echo message="error-prop: ${error-prop}"/>
    <property name="error-ref-prop" refid="error-ref" />
    <echo message="From reference: ${error-ref-prop}"/>


Run without specialError.

Run with specialError.

Done =)

Reference: Ant-contrib Tasks: Trycatch


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.