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

# 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"
}
```
