# 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: 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.jobresult/jobresult/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.
