800-596-4880 ](tel:1-800-596-4880)

Online  [ Contact Us ](https://www.mulesoft.com/contact)

  * Login 

[ Anypoint Platform
](https://anypoint.mulesoft.com/login/#/signin?apintent=generic) [ Composer
](https://composer.mulesoft.com/login/sign-in) [ Training
](https://training.mulesoft.com/login) [ Help Center
](https://help.mulesoft.com/s/login/)

[ Free trial ](https://anypoint.mulesoft.com/login/#/signup?apintent=generic)

[ Link to MuleSoft Twitter profile  ](https://twitter.com/MuleSoft) [ Link to
MuleSoft Linkedin profile  ](https://www.linkedin.com/company/mulesoft) [ Link
to MuleSoft Facebook page  ](https://www.facebook.com/MuleSoft) [ Link to
MuleSoft Instagram profile  ](https://www.instagram.com/mulesoft/) [ Link to
MuleSoft Videos platform  ](https://videos.mulesoft.com/) [ Link to MuleSoft
Twitch profile  ](https://www.twitch.tv/mulesoft_community)

© Copyright 2024  Salesforce, Inc. [ All rights reserved
](https://www.salesforce.com/company/legal/intellectual/) .

![](../../_/img/icons/arrow-down.svg)

Skip to main content

[ View on GitHub ](https://github.com/mulesoft/docs-
dataweave/blob/v2.6/modules/ROOT/pages/dw-runtime-functions-try.adoc)

#  On this page:

Menu

  1. [ Homepage  ](../../general/)
  2. [ DataWeave (2.6) ](./)
  3. [ DataWeave Reference ](dw-functions)
  4. [ dw::Runtime ](dw-runtime)
  5. try 

![](../../_/img/icons/arrow-down.svg) ![](../../_/img/icons/search.svg)

  1. [ Homepage  ](../../general/)
  2. [ DataWeave (2.6) ](./)
  3. [ DataWeave Reference ](dw-functions)
  4. [ dw::Runtime ](dw-runtime)
  5. try 

#  try

##  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

Name  |  Description   
---|---  
` delegate ` |  The function to evaluate.   
  
###  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

    
    
    %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:

    
    
    {
      "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:

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

Back to top

Did this article solve your issue?

Let us know so we can improve!

Yes

![](../../_/img/icons/like.svg)

No

![](../../_/img/icons/dislike.svg)

Please check at least 1 checkbox.

Would you like to share any additional feedback? (Optional)

![](../../_/img/icons/success.svg) Your feedback has been successfully
submitted. Thank you!  [ View on GitHub ](https://github.com/mulesoft/docs-
dataweave/blob/v2.6/modules/ROOT/pages/dw-runtime-functions-try.adoc)

  * Products 
    * [ Anypoint Platform ](https://www.mulesoft.com/platform/enterprise-integra