Summary
- Assembly
- NAnt
.Core .dll - Namespace
- NAnt
.Core .Tasks - Interfaces
- Base Types
Syntax
[TaskName("trycatch")]
public class TryCatchTask : Task, IConditional
Examples
<trycatch>
<try>
<echo message="In try" />
<fail message="Failing!" />
</try>
<catch>
<echo message="In catch" />
</catch>
<finally>
<echo message="Finally done" />
</finally>
</trycatch>
The output of this example will be:
In try
In catch
Finally done
The failure in the < block will
not cause the build to fail.
<trycatch>
<try>
<echo message="In try" />
<fail message="Just because..." />
</try>
<catch property="failure">
<echo message="Caught failure: ${failure}" />
<fail message="Bad catch" />
</catch>
<finally>
<echo message="Finally done" />
</finally>
</trycatch>
The output of this example will be:
In try
Caught failure: Just because...
Finally done
Build failed: Bad catch
Like the above, the failure in the <
block does not cause the build to fail. The failure in the
< block does, however.
Note that the < block is
executed even though the <
block failed.
<trycatch>
<try>
<echo message="In try" />
<fail message="yet again" />
</try>
<catch property="failure">
<echo message="Caught failure ${failure}" />
<fail message="Bad catch" />
</catch>
<finally>
<echo message="Finally done ${failure}" />
</finally>
</trycatch>
The output of this example will be:
In try
Caught failure yet again
Build failed: Property 'failure' has not been set.
The NAnt.Core.Tasks.EchoTask in the <
block failed because the "failure" property was not defined
after exiting the < block.
Note that the failure in the <
block has eclipsed the failure in the <
block.
<trycatch>
<try>
<property name="temp.file" value="${path::get-temp-file-name()}" />
<do-stuff to="${temp.file}" />
<fail message="Oops..." />
</try>
<finally>
<echo message="Cleaning up..." />
<if test="${property::exists('temp.file')}">
<delete file="${temp.file}" />
</if>
</finally>
</trycatch>
A more concrete example, that will always clean up the generated temporary file after it has been created.
Remarks
The tasks defined in the < block
will be executed in turn, as they normally would in a target.
If a < block is defined, the
tasks in that block will be executed in turn only if one of the tasks
in the < block fails. This
failure will then be suppressed by the <
block.
The message associated with the failure can also be caught in a
property for use within the <
block. The original contents of the property will be restored upon
exiting the < block.
If a < block is defined, the
tasks in that block will be executed after the tasks in both the
< and <
blocks have been executed, regardless of whether any task fails in
either block.
Attributes
| Type | Description |
|---|---|
| TaskNameAttribute | Indicates that class should be treated as a task. |
Properties
| Name | Value | Summary |
|---|---|---|
| CatchBlock | TryCatchTask |
The tasks in this block will be executed if any task in the try
block fails.
|
| CustomXmlProcessing | bool |
Gets a value indicating whether the element is performing additional
processing using the
NAnt.Core.Element.XmlNode that was used to
initialize the element.
Inherited from Element
|
| FailOnError | bool |
Determines if task failure stops the build, or is just reported.
The default is
true.
Inherited from Task
|
| FinallyBlock | ElementContainer |
The tasks in this block will always be executed, regardless of
what happens in the try and catch blocks.
|
| IfDefined | bool |
If
true then the task will be executed; otherwise,
skipped. The default is true.
Inherited from Task
|
| Location | Location |
Gets or sets the location in the build file where the element is
defined.
Inherited from Element
|
| LogPrefix | string |
The prefix used when sending messages to the log.
Inherited from Task
|
| Name | string |
The name of the task.
Inherited from Task
|
| NamespaceManager | XmlNamespaceManager |
Gets or sets the
System.Xml.XmlNamespaceManager.
Inherited from Element
|
| Parent | Object |
Gets or sets the parent of the element.
Inherited from Element
|
| Project | Project |
Gets or sets the
NAnt.Core.Element.Project to which this element belongs.
Inherited from Element
|
| Properties | PropertyDictionary |
Gets the properties local to this
NAnt.Core.Element and the
NAnt.Core.Element.Project.
Inherited from Element
|
| Threshold | Level |
Gets or sets the log threshold for this
NAnt.Core.Task. By
default the threshold of a task is NAnt.Core.Level.Debug,
causing no messages to be filtered in the task itself.
Inherited from Task
|
| TryBlock | ElementContainer |
The tasks in this block will be executed as a normal part of
the build script.
|
| UnlessDefined | bool |
Opposite of
NAnt.Core.Task.IfDefined. If false
then the task will be executed; otherwise, skipped. The default is
false.
Inherited from Task
|
| Verbose | bool |
Determines whether the task should report detailed build log messages.
The default is
false.
Inherited from Task
|
| XmlNode | XmlNode |
Gets or sets the XML node of the element.
Inherited from Element
|
Methods
| Name | Value | Summary |
|---|---|---|
| CopyTo |
void |
Copies all instance data of the
NAnt.Core.Element to a given
NAnt.Core.Element.
Inherited from Element
|
| Execute |
void |
Executes the task unless it is skipped.
Inherited from Task
|
| ExecuteTask |
void |
Executes the task.
|
| Get |
XmlNode |
Locates the XML node for the specified attribute in either the
configuration section of the extension assembly or the.project.
Inherited from Task
|
| Get |
XmlNode |
Inherited from Element
|
| GetLocation |
Location |
Retrieves the location in the build file where the element is
defined.
Inherited from Element
|
| Initialize |
void |
Initializes the task.
Inherited from Task
|
| Initialize |
void |
Performs default initialization.
Inherited from Element
|
| Initialize |
Element |
Initializes the build element.
Inherited from Element
static
|
| InitializeElement |
void |
Derived classes should override to this method to provide extra
initialization and validation not covered by the base class.
Inherited from Element
|
| InitializeTask |
void |
Initializes the task.
Inherited from Task
|
| Initialize |
void |
Initializes the configuration of the task using configuration
settings retrieved from the NAnt configuration file.
Inherited from Task
|
| InitializeXml |
void |
Initializes all build attributes and child elements.
Inherited from Element
|
| IsLogEnabledFor |
bool |
Determines whether build output is enabled for the given
NAnt.Core.Level.
Inherited from Task
|
| Log |
void |
Logs a message with the given priority.
Inherited from Task
|
| Log |
void |
Logs a formatted message with the given priority.
Inherited from Task
|
