---
schema_version: "1"
id: 259566fa155b07f7da0ce37836194daf2f0fc8a3feadacce85d7ef2981612dc1
title: try
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dw-runtime-functions-try.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dw-runtime-functions-try
source_site: docs.mulesoft.com
content_type: documentation
content_hash: d3ba6411d18c98f204e22dc90fbcdf84f0b770d0791fd1ad59293b046fc2f37f
description: MuleSoft Documentation Site
release: "2.13"
latest: true
---
---
product: DataWeave
version: 2.13
is-latest-version: true
---

# try

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## try<T>(delegate: () -> T): TryResult<T>

Evaluates the delegate function and returns an object with `success: true` and `result` if the
delegate function succeeds, or an object with `success: false` and `error` if the delegate function
throws an exception.

The `orElseTry` and `orElse` functions will also continue processing if the `try` function fails.
See the `orElseTry` and `orElse` documentation for more complete examples of handling failing `try`
function expressions.

Note: Instead of using the `orElseTry` and `orElse` functions, based on the output of the `try`
function, you can add conditional logic to execute when the result is `success: true` or
`success: false`.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>delegate</code></p></td><td><p>The function to evaluate.</p></td></tr></tbody></table>

### Example

This example calls the `try` function using the `randomNumber` function as argument. The function
`randomNumber` generates a random number and calls `fail` if the number is greater than 0.5. The
declaration of this function is in the script’s header.

#### Source

```DataWeave
%dw 2.0
import try, fail from dw::Runtime
output application/json
fun randomNumber() =
if(random() > 0.5)
  fail("This function is failing")
 else
  "OK"
---
try(() -> randomNumber())
```

#### Output

When `randomNumber` fails, the output is:

```JSON
{
  "success": false,
  "error": {
    "kind": "UserException",
    "message": "This function is failing",
    "location": "Unknown location",
    "stack": [
      "fail (anonymous:0:0)",
      "myFunction (anonymous:1:114)",
      "main (anonymous:1:179)"
    ]
  }
}
```

When `randomNumber` succeeds, the output is:

```JSON
{
  "success": true,
  "result": "OK"
}
```
