> 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.jobresult/jobresult/catch.md).

# catch

### JobResult.catch

Namespace: `FsToolkit.ErrorHandling`

Function Signature:

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

Catches exceptions that occur during the execution of a `Job<Result<'a, 'b>>` and maps them to the `Error` case using the provided function.

### Examples

#### Example 1

```fsharp
let riskyJob : Job<Result<int, string>> =
    job { return failwith "unexpected failure" }

riskyJob
|> JobResult.catch (fun ex -> ex.Message)
// job { return Error "unexpected failure" }
```

#### Example 2

```fsharp
let safeDivide (x: int) (y: int) : Job<Result<int, string>> =
    job { return Ok (x / y) }

safeDivide 10 0
|> JobResult.catch (fun ex -> sprintf "Division error: %s" ex.Message)
// job { return Error "Division error: Attempted to divide by zero." }
```

#### Example 3

```fsharp
let fetchData : Job<Result<string, AppError>> = // ...

fetchData
|> JobResult.catch (fun ex -> AppError.Unexpected ex)
// Exceptions become Error(AppError.Unexpected ex)
// Existing errors pass through unchanged
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
