# catch

### TaskResult.catch

Namespace: `FsToolkit.ErrorHandling`

Catches exceptions and maps them to the Error case using the provided function.

Function Signature:

```fsharp
(exn -> 'a) -> Task<Result<'b, 'a>> -> Task<Result<'b, 'a>>
```

### Examples

Given the function:

```fsharp
let taskThrow () =
  task {
    failwith "an exception happened"
    return Error ""
  }
```

#### Example 1

```fsharp
let result : Task<Result<int, string>> = TaskResult.catch (_.Message) (TaskResult.ok 42)
// task { Ok 42 }
```

#### Example 2

```fsharp
let result : Task<Result<int, string>> = TaskResult.catch (_.Message) (TaskResult.error "something bad happened")
// task { Error "something bad happened" }
```

#### Example 3

```fsharp
let result : Task<Result<int, string>> = TaskResult.catch (_.Message) (taskThrow ())
// task { Error "an exception happened" }
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/taskresult/catch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
