Ant trycatch usage

We recently spent much time debugging a build issue that resulted from an Ant-Contrib <trycatch>. Due to a missing <catch> element, an error that occurred within the <try>element went unreported.

After some investigation, we’d like to suggest the following usage:

  • Add a “reference” attribute to the <trycatch>element
  • Add a <catch> element to the <trycatch>block in order to:
    • Create a <property>based on the exception reference
    • Create another <property> containing a formatted message based on the exception <property>
    • <echo>the message, which will appear where the error occurred in the Ant output
    • <fail> specifying the message, which will ensure that a stack trace (of sorts) will appear at the end of the Ant output
<trycatch reference="exception_ref" >
		<try>
			... some risky Ant work ...
		</try>
		<catch>
			<property name="exception" refid="exception_ref" />
			<property name="message" value="Error in trycatch block:${line.separator}${exception}" />
			<echo message="${message}" />
			<fail message="${message}" />
		</catch>
		<finally>
			... optional ...
		</finally>
</trycatch>

Note

The <trycatch> element also supports a “property” attribute that will ‘receive the message of the exception’, and as such loses the stack information. By contrast, the “reference”attribute points to the exception object itself, including the stack information.

See http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html.

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply