---
schema_version: "1"
id: e800b2728cbb281b97171fa422c32e992a2fef4fa98897c63de37e71e2a8c97f
title: try
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.6/dw-runtime-functions-try.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.6/dw-runtime-functions-try
source_site: docs.mulesoft.com
content_type: documentation
content_hash: d4408a0192c4858a1cb8e1677855f5091d2e3540a39bc301457f20204604ab01
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
# try

## [](#try1)try<T>(delegate: () -> T): TryResult<T>

デリゲート関数を評価し、デリゲート関数が成功した場合は ​`success: true`​ と ​`result`​ を含むオブジェクトを返し、デリゲート関数が例外をスローした場合は ​`success: false`​ と ​`error`​ を含むオブジェクトを返します。

`try`​ 関数が失敗しても、​`orElseTry`​ 関数と ​`orElse`​ 関数は処理を継続します。失敗した ​`try`​ 関数式の処理の詳細な例は、​`orElseTry`​ と ​`orElse`​ のドキュメントを参照してください。

注意: `orElseTry`​ 関数と ​`orElse`​ 関数を使用する代わりに、​`try`​ 関数の出力に基づいて、結果が ​`success: true`​ または ​`success: false`​ の場合に実行する条件ロジックを追加できます。

### [](#パラメーター)パラメーター

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 75%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">名前</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>delegate</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">評価する関数。</p></td>
</tr>
</tbody>
</table>

### [](#例)例

次の例では、​`randomNumber`​ 関数を引数として使用し、​`try`​ 関数をコールしています。 関数 ​`randomNumber`​ は乱数を生成し、数値が 0.5 より大きい場合は ​`fail`​ をコールします。この関数の宣言はスクリプトのヘッダーにあります。

#### [](#ソース)ソース

```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())
```

#### [](#出力)出力

`randomNumber`​ が失敗すると、出力は次のようになります。

```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)"
    ]
  }
}
```

`randomNumber`​ が成功すると、出力は次のようになります。

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