public class TryCatchTask
extends org.apache.tools.ant.Task
This mirrors Java's try/catch/finally.
The tasks inside of the required <try>
element will be run. If one of them should throw a BuildException
several things
can happen:
<catch>
block, the
exception will be passed through to Ant.<catch>
block, the tasks
nested into it will be run.If a <finally>
block is present, the task
nested into it will be run, no matter whether the first tasks have
thrown an exception or not.
Attributes:
Name | Description | Required |
property | Name of a property that will receive the message of the exception that has been caught (if any) | No |
reference | Id of a reference that will point to the exception object that has been caught (if any) | No |
Use the following task to define the <trycatch>
task before you use it the first time:
<taskdef name="trycatch"
classname="net.sf.antcontrib.logic.TryCatchTask" />
<trycatch property="foo" reference="bar">
<try>
<fail>Tada!</fail>
</try>
<catch>
<echo>In <catch>.</echo>
</catch>
<finally>
<echo>In <finally>.</echo>
</finally>
</trycatch>
<echo>As property: ${foo}</echo>
<property name="baz" refid="bar" />
<echo>From reference: ${baz}</echo>
results in
[trycatch] Caught exception: Tada!
[echo] In <catch>.
[echo] In <finally>.
[echo] As property: Tada!
[echo] From reference: Tada!
Modifier and Type | Class and Description |
---|---|
static class |
TryCatchTask.CatchBlock |
Constructor and Description |
---|
TryCatchTask() |
Modifier and Type | Method and Description |
---|---|
void |
addCatch(TryCatchTask.CatchBlock cb) |
void |
addFinally(org.apache.tools.ant.taskdefs.Sequential seq)
Adds a nested <finally> block - at most one is allowed.
|
void |
addTry(org.apache.tools.ant.taskdefs.Sequential seq)
Adds a nested <try> block - one is required, more is
forbidden.
|
void |
execute()
The heart of the task.
|
void |
setProperty(java.lang.String p)
Sets the property attribute.
|
void |
setReference(java.lang.String r)
Sets the reference attribute.
|
bindToOwner, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, log, log, maybeConfigure, perform, reconfigure, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType
public void addTry(org.apache.tools.ant.taskdefs.Sequential seq) throws org.apache.tools.ant.BuildException
org.apache.tools.ant.BuildException
public void addCatch(TryCatchTask.CatchBlock cb)
public void addFinally(org.apache.tools.ant.taskdefs.Sequential seq) throws org.apache.tools.ant.BuildException
org.apache.tools.ant.BuildException
public void setProperty(java.lang.String p)
public void setReference(java.lang.String r)
public void execute() throws org.apache.tools.ant.BuildException
execute
in class org.apache.tools.ant.Task
org.apache.tools.ant.BuildException