> For the complete documentation index, see [llms.txt](https://demystifyfp.gitbook.io/fstoolkit-errorhandling/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/taskresult/transforms/ofcatchtask.md).

# ofCatchTask

Namespace: `FsToolkit.ErrorHandling`

Transforms a `Task<'T>` into a `Task<Result<'T, exn>>`, catching any exceptions thrown by the task and wrapping them in the `Error` case.

Unlike [`ofTask`](/fstoolkit-errorhandling/fstoolkit.errorhandling/taskresult/transforms/oftask.md), this function catches exceptions that escape from the task and maps them to `Error`. If the task completes successfully, the result is wrapped in `Ok`.

## Function Signature

```fsharp
Task<'T> -> Task<Result<'T, exn>>
```

## Examples

### Example 1

```fsharp
let result = TaskResult.ofCatchTask (task { return 42 })
// task { return Ok 42 }
```

### Example 2

```fsharp
let result = TaskResult.ofCatchTask (task { failwith "something went wrong" })
// task { return Error (System.Exception("something went wrong")) }
```
