---
title: "Catch"
language: "en"
type: "Symbol"
summary: "Catch[expr] returns the argument of the first Throw generated in the evaluation of expr. Catch[expr, form] returns value from the first Throw[value, tag] for which form matches tag. Catch[expr, form, f] returns f[value, tag]."
keywords: 
- computed goto
- control
- divert
- errors
- handling of errors
- jump
- longjmp
- non-local returns
- scope
- stop
- CATCH
- ON_ERROR
- catch
- finally
- try
- catch
- lasterror
- traperror
- catch
- try
canonical_url: "https://reference.wolfram.com/language/ref/Catch.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Flow Control"
    link: "https://reference.wolfram.com/language/guide/FlowControl.en.md"
  - 
    title: "Looping Constructs"
    link: "https://reference.wolfram.com/language/guide/LoopingConstructs.en.md"
  - 
    title: "Scoping Constructs"
    link: "https://reference.wolfram.com/language/guide/ScopingConstructs.en.md"
  - 
    title: "Procedural Programming"
    link: "https://reference.wolfram.com/language/guide/ProceduralProgramming.en.md"
  - 
    title: "Language Overview"
    link: "https://reference.wolfram.com/language/guide/LanguageOverview.en.md"
  - 
    title: "Robustness & Error Handling"
    link: "https://reference.wolfram.com/language/guide/RobustnessAndErrorHandling.en.md"
related_functions: 
  - 
    title: "Throw"
    link: "https://reference.wolfram.com/language/ref/Throw.en.md"
  - 
    title: "Enclose"
    link: "https://reference.wolfram.com/language/ref/Enclose.en.md"
  - 
    title: "Check"
    link: "https://reference.wolfram.com/language/ref/Check.en.md"
  - 
    title: "CheckAbort"
    link: "https://reference.wolfram.com/language/ref/CheckAbort.en.md"
  - 
    title: "Reap"
    link: "https://reference.wolfram.com/language/ref/Reap.en.md"
related_tutorials: 
  - 
    title: "Loops and Control Structures"
    link: "https://reference.wolfram.com/language/tutorial/EvaluationOfExpressions.en.md#11091"
---
# Catch

Catch[expr] returns the argument of the first Throw generated in the evaluation of expr. 

Catch[expr, form] returns value from the first Throw[value, tag] for which form matches tag. 

Catch[expr, form, f] returns f[value, tag].

## Details

* ``Catch[expr, …]`` always returns the value of ``expr`` if no ``Throw`` was generated during the evaluation.

* ``form`` can be any expression, and is often a pattern.

* ``tag`` in ``Throw[value, tag]`` is re‐evaluated every time it is compared to ``form``.

---

## Examples (15)

### Basic Examples (3)

Exit to the enclosing ``Catch`` as soon as the ``Throw`` is evaluated:

```wl
In[1]:= Catch[a;b;Throw[c];d;e]

Out[1]= c
```

---

Define a function that can "throw an exception":

```wl
In[1]:= f[x_] := If[x > 10, Throw[overflow], x!]
```

The result of the ``Catch`` is just what is thrown by ``Throw`` :

```wl
In[2]:= Catch[f[2] + f[11]]

Out[2]= overflow

In[3]:= Catch[f[2] + f[3]]

Out[3]= 8
```

---

Use ``Throw`` to exit a loop when a criterion is satisfied:

```wl
In[1]:= Catch[Do[If[i! > 10 ^ 10, Throw[i]], {i, 100}]]

Out[1]= 14
```

### Scope (5)

``Catch`` can catch a ``Throw`` from inside essentially any function:

```wl
In[1]:= Catch[If[# < 0, Throw[#]]& /@ {1, 2, 0, -1, 5, 6}]

Out[1]= -1

In[2]:= Catch[{a, Throw[b], c}]

Out[2]= b

In[3]:= Catch[a ^ 2 + b ^ 2 + c ^ 2 /. b :> Throw[bbb]]

Out[3]= bbb
```

---

The nearest enclosing ``Catch`` catches the ``Throw`` :

```wl
In[1]:= Catch[{Catch[{a, Throw[b], c}], d, e}]

Out[1]= {b, d, e}
```

---

``Catch`` picks up the first ``Throw`` that is evaluated:

```wl
In[1]:= Catch[{Throw[a], Throw[b], Throw[c]}]

Out[1]= a

In[2]:= Catch[Throw /@ {a, b, c}]

Out[2]= a
```

---

``Throw`` need not occur lexically inside ``Catch`` :

```wl
In[1]:= f[x_] := (If[x < 0, Throw["negative"]];Sqrt[x])

In[2]:= Catch[Sum[f[i], {i, 5, -5, -1}]]

Out[2]= "negative"
```

---

A function that can throw a number of different exceptions:

```wl
In[1]:=
f[x_] := Which[
	x < 0, Throw[x, error[negative]], 
	x == 0, Throw[x, error[zero]], 
	True, 1 / Sqrt[x]]
```

A handler for the possible exceptions:

```wl
In[2]:=
ff[x_] := 
	Catch[f[x], error[_], Function[{value, tag}, tag /. {error[negative] :> Indeterminate, error[zero] :> Infinity, _ :> Throw[value, tag]}]]

In[3]:= ff /@ {-1, 0, 1}

Out[3]= {Indeterminate, ∞, 1}
```

### Generalizations & Extensions (3)

Catch the ``Throw`` with tag ``u`` :

```wl
In[1]:= Catch[Throw[a, u], u]

Out[1]= a
```

---

The inner ``Catch`` catches the ``Throw`` :

```wl
In[1]:= Catch[f[Catch[Throw[a, u], u]], v]

Out[1]= f[a]
```

The outer ``Catch`` catches the ``Throw`` :

```wl
In[2]:= Catch[f[Catch[Throw[a, u], v]], u]

Out[2]= a
```

---

Keep the tag local:

```wl
In[1]:= Module[{u}, Catch[Throw[a, u], u]]

Out[1]= a
```

### Applications (3)

Find the next prime after ``10^10`` :

```wl
In[1]:= Catch[Do[If[PrimeQ[i], Throw[i]], {i, 10 ^ 10, 10 ^ 10 + 1000}]]

Out[1]= 10000000019
```

---

Find the first power of 17 equal to 1 mod 19:

```wl
In[1]:= Catch[Do[If[Mod[17 ^ i, 19] == 1, Throw[i]], {i, 19}]]

Out[1]= 9
```

---

Stop if an iteration gets too large:

```wl
In[1]:= NestList[# ^ 2 + 1&, 2, 6]

Out[1]= {2, 5, 26, 677, 458330, 210066388901, 44127887745906175987802}

In[2]:= Catch[NestList[If[# > 1000, Throw[#], # ^ 2 + 1]&, 2, 6]]

Out[2]= 458330
```

### Properties & Relations (1)

Use ``Check`` to throw an exception if a message is generated:

```wl
In[1]:= Catch[2 + Check[1 / (1 + 1 / 0), Throw[error]]]
```

Power::infy: Infinite expression 1/0 encountered.

```wl
Out[1]= error

In[2]:= Catch[2 + Check[1 / (1 + 1 / 2), Throw[error]]]

Out[2]= (8/3)
```

## See Also

* [`Throw`](https://reference.wolfram.com/language/ref/Throw.en.md)
* [`Enclose`](https://reference.wolfram.com/language/ref/Enclose.en.md)
* [`Check`](https://reference.wolfram.com/language/ref/Check.en.md)
* [`CheckAbort`](https://reference.wolfram.com/language/ref/CheckAbort.en.md)
* [`Reap`](https://reference.wolfram.com/language/ref/Reap.en.md)

## Tech Notes

* [Loops and Control Structures](https://reference.wolfram.com/language/tutorial/EvaluationOfExpressions.en.md#11091)

## Related Guides

* [Flow Control](https://reference.wolfram.com/language/guide/FlowControl.en.md)
* [Looping Constructs](https://reference.wolfram.com/language/guide/LoopingConstructs.en.md)
* [Scoping Constructs](https://reference.wolfram.com/language/guide/ScopingConstructs.en.md)
* [Procedural Programming](https://reference.wolfram.com/language/guide/ProceduralProgramming.en.md)
* [Language Overview](https://reference.wolfram.com/language/guide/LanguageOverview.en.md)
* [Robustness & Error Handling](https://reference.wolfram.com/language/guide/RobustnessAndErrorHandling.en.md)

## Related Links

* [Fast Introduction for Programmers: Procedures](http://www.wolfram.com/language/fast-introduction-for-programmers/procedures/)
* [NKS\|Online](http://www.wolframscience.com/nks/search/?q=Catch)
* [A New Kind of Science](http://www.wolframscience.com/nks/)

## History

* Introduced in 1988 (1.0) \| Updated in 1996 (3.0)