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>
Done =)
Reference: Ant-contrib Tasks: Trycatch


