Chapter 7. Try Task

The "Try" task works similarly to the try/catch/finally construct in Java. This task is useful when a particular task might fail, but the build should not fail if it does. An example is the "mail" task will fail if the mail server is not available, but the build should not fail if the mail message cannot be delivered.

To use this task in your build files, include a task definition like this:


<taskdef name="try" classname="ise.antelope.tasks.TryTask"/>

A quick example is probably all that is necessary:


 <tempfile property="temp.file" destdir="${java.io.tmpdir}"
     prefix="delete" suffix=".tmp"/>
 <try>
     <!-- use 'get' task to post to the unit test status servlet. It
         would be better to use a post for this, but this shows a good
         use of 'finally'. -->
     <get
         src="http://mycompany.com/servlet/junit?testnum=${test.num}&status="${status}"
         dest="${temp.file}"/>

     <catch>
         <echo>Unit test servlet update failed.</echo>
     </catch>

     <finally>
         <delete file="${temp.file}"/>
     </finally>
 </try>

Unlike the Java "try", neither the "catch" block nor the "finally" block are required. Also, the order does not matter, the "catch" block may be listed first, followed by the "finally", followed by the tasks that may fail.

Table 7.1. Try Task Attributes

AttributeDescriptionDefaultRequired
breakIf true and a nested task fails, no other nested tasks will execute. If false, all nested tasks will execute regardless of whether a previous task failed. Note that for each failed task, the 'catch' block (if defined) will execute.trueNo
printstacktraceIf true, the exception stack trace from a failed task will be logged. falseNo
stacktracepropertySpecify a property to store the stack trace of any exception. NoneNo
printmessageIf true, the exception message from a failed task will be logged. If printstacktrace is set to true, this attribute is ignored as the exception message is printed as part of the stack trace.trueNo
messagepropertySpecify a property to store the message line of any exception.NoneNo

The next example shows the "break" attribute set to "no". In this case, the second echo task will execute.


    <target name="test" description="This exercises the Try task.">
        <try break="no">
            <echo>I am trying...</echo>
            <fail message=" and I failed..."/>
            <echo> but I did not die!</echo> <!-- this WILL print -->
        </try>
    </target>
   

This slightly more practical example uses the Variable task coupled with "try" to run a series of tests. All tests will run even if a preceding test fails. The "catch" block logs a message of each failed test.


    <target name="runTests" messageproperty="msg">
        <try catch="testFailed" break="no">
            <var name="testname" value="fileUtilTests"/>
            <antcall target="runFileUtilTests"/>
            <var name="testname" value="imageUtilTests"/>
            <antcall target="runImageUtilTests"/>
            <var name="testname" value="imageConversionTests"/>
            <antcall target="runImageConversionTests"/>

            <catch>
              <!-- log a test failure -->
              <echo file="test.log" append="yes">
                  Test suite ${testname} failed: ${msg}
              </echo>
            </catch>
        </try>
    </target>
    </target>
   

The following example uses a nested Finally to clean up resources:


    <tempfile property="temp.file" destdir="${java.io.tmpdir}"
        prefix="delete" suffix=".tmp"/>
    <try>
        <!-- use 'get' task to post to the unit test status servlet. It
            would be better use use a post for this, but this shows a good
            use of 'finally'. -->
        <get
            src="http://mycompany.com/servlet/junit?testnum=${test.num}&status="${status}"
            dest="${temp.file}"/>

        <catch>
            <echo>Unit test servlet update failed.</echo>
        </catch>

        <finally>
            <delete file="${temp.file}"/>
        </finally>
    </try>

See the post task for a better way to do a post.