B.34 TryCatchTask
Prev | Appendix B. Core tasks | Next

## B.34 TryCatchTask

This task is a wrapper task that lets you run tasks(s) when another set of tasks fails, mirroring PHP's `try/catch` functionality (with the addition of `finally` block)

The tasks inside of the `try` block will always be run. If one of them throws a BuildException, the following things can happen:

- If there is no `catch` block, the exception will be passed to Phing.

- If the `property` attribute has been set a property of that name will contain the message of the exception.

- If there is a `catch` block, the nested tasks will be run.

If a `finally` block is present, the nested tasks will be run regardless of whether the tasks in the `try` block have thrown an exception or not.

This task was inspired by http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html.

Table B.36: Attributes

Name | Type | Description | Default | Required
--- | --- | --- | --- | ---
`property` | `String` | Name of a property that will receive the message of the exception that has been caught (if any) | n/a | No

### B.34.1 Examples

    <trycatch property="foo">
        <try>
            <fail>Tada!</fail>
        </try>

        <catch>
            <echo>In catch.</echo>
        </catch>

        <finally>
            <echo>In finally.</echo>
        </finally>
    </trycatch>

    <echo>As property: ${foo}</echo>

Prev | Up | Next
B.33 TouchTask | Home | B.35 TstampTask