You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Daniels, Doug" <Do...@gdc4s.com> on 2004/05/19 16:01:03 UTC

RE: Error Handling

You can use the ant-contrib (http://ant-contrib.sourceforge.net/) <trycatch> task (http://ant-contrib.sourceforge.net/tasks/trycatch.html), "Trytask is a wrapper that lets you run a set of tasks and optionally run a different set of tasks if the first set fails and yet another set after the first one has finished."

For example:

<target name="taskFailCheck">
<trycatch property="taskFailed" reference="errorRef">
  <try>
    <antcall target="normalTarget" />
  </try>

  <catch>
    <echo>Error caught: ${errorRef}</echo>
  </catch>

  <finally>
    <echo>Check if we failed and perform task otherwise perform other task.</echo>

    <if> <isset property="taskFailed" /> <then>
 
       <antcall target="failTarget" />

    </then>

<!-- otherwise perform succesful task -->
    <else>

       <antcall target="successTarget" />

    </else>
    </if>

  </finally>
</trycatch>
</target>

You just replace the normalTarget, failTarget, and successTarget with your targets, you could also just check the taskFailed property in the other targets themselves, for example:

<target name="successTarget" unless="taskFailed">

or

<target name="failedTarget" if="taskFailed">

then you'd have a target setup like this to setup the order:

<target name="masterTarget" depends="taskFailCheck, successTarget, failedTarget"/>

This task would run your trycatch then it would run either the successTarget or the failedTarget depending on the success and failure of the normalTarget target.


To use ant-contrib tasks. First Download the ant-contrib tasks jar and then at the top of your build file you must declare the ant-contrib tasks:
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
  <classpath>
    <pathelement location="/usr/share/java/lib/ant-contrib-0.3.jar"/>
  </classpath>
</taskdef>



-----Original Message-----
From: Thirumala.Thoka@fairchildsemi.com
[mailto:Thirumala.Thoka@fairchildsemi.com]
Sent: Monday, May 17, 2004 4:58 PM
To: dev@ant.apache.org
Subject: Error Handling


Hi All,

How do we capture the error on ANT target, if it does success do one thing 
otherwise do other thing. So, basically how do we code this requirement.

I did little bit of try with "failureonerror", "haltonerror" attributes 
but it's not accepting those attributes at all.
I would appreciate your ealiest response. Pls. do ask me if you need more 
details about the question.

Regards,
Thiru

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org