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">
    <classpath>
      <pathelement location="${basedir}/ant-lib/ant-contrib-1.0b3.jar"/>
    </classpath>
  </taskdef>

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

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

      <finally>
        <echo message="finally"/>
      </finally>
    </trycatch>

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

 

Run without specialError.
ant-try-catch-example-1
 

Run with specialError.
ant-try-catch-example-2
 

Done =)

Reference: Ant-contrib Tasks: Trycatch

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s