---
title: "Throw"
language: "en"
type: "Symbol"
summary: "Throw[value] stops evaluation and returns value as the value of the nearest enclosing Catch. Throw[value, tag] is caught only by Catch[expr, form], where tag matches form. Throw[value, tag, f] returns f[value, tag] as the top-level value if no appropriate Catch is found."
keywords: 
- computed goto
- control
- divert
- errors
- handling of errors
- jump
- longjmp
- non-local returns
- scope
- stop
- finally
- throw
- try
- throw
- error
- rethrow
canonical_url: "https://reference.wolfram.com/language/ref/Throw.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: "Catch"
    link: "https://reference.wolfram.com/language/ref/Catch.en.md"
  - 
    title: "Confirm"
    link: "https://reference.wolfram.com/language/ref/Confirm.en.md"
  - 
    title: "Return"
    link: "https://reference.wolfram.com/language/ref/Return.en.md"
  - 
    title: "Goto"
    link: "https://reference.wolfram.com/language/ref/Goto.en.md"
  - 
    title: "Interrupt"
    link: "https://reference.wolfram.com/language/ref/Interrupt.en.md"
  - 
    title: "Abort"
    link: "https://reference.wolfram.com/language/ref/Abort.en.md"
  - 
    title: "Sow"
    link: "https://reference.wolfram.com/language/ref/Sow.en.md"
  - 
    title: "NestWhile"
    link: "https://reference.wolfram.com/language/ref/NestWhile.en.md"
  - 
    title: "For"
    link: "https://reference.wolfram.com/language/ref/For.en.md"
  - 
    title: "While"
    link: "https://reference.wolfram.com/language/ref/While.en.md"
  - 
    title: "Until"
    link: "https://reference.wolfram.com/language/ref/Until.en.md"
  - 
    title: "AskConfirm"
    link: "https://reference.wolfram.com/language/ref/AskConfirm.en.md"
related_tutorials: 
  - 
    title: "Loops and Control Structures"
    link: "https://reference.wolfram.com/language/tutorial/EvaluationOfExpressions.en.md#11091"
  - 
    title: "Messages"
    link: "https://reference.wolfram.com/language/tutorial/TextualInputAndOutput.en.md#12413"
---
# Throw

Throw[value] stops evaluation and returns value as the value of the nearest enclosing Catch.

Throw[value, tag] is caught only by Catch[expr, form], where tag matches form.

Throw[value, tag, f] returns f[value, tag] as the top-level value if no appropriate Catch is found.

## Details

* You can use ``Throw`` and ``Catch`` to exit functions such as ``Nest``, ``Fold``, ``FixedPoint``, and ``Scan``.

* ``tag`` can be any expression.

* ``tag`` in ``Throw[value, tag]`` is reevaluated every time it is compared to ``form`` in ``Catch[expr, form]``.

* An error is generated and an unevaluated ``Throw`` is returned if there is no appropriate enclosing ``Catch`` to catch the ``Throw``.

---

## Examples (16)

### Basic Examples (3)

Exit to the enclosing ``Catch`` as soon as ``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 ``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 (4)

``Throw`` works 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
```

---

Use ``Throw`` to throw any type of expression:

```wl
In[1]:= Catch[If[NumericQ[#], Throw[#]]& //@ Integrate[1 / (x ^ 5 - 1), x]]

Out[1]= (1/20)
```

---

``Throw`` is caught by the nearest enclosing ``Catch`` :

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

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

---

As soon as the first ``Throw`` is evaluated, it exits to the enclosing ``Catch`` :

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

Out[1]= a

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

Out[2]= a
```

### Generalizations & Extensions (3)

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

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

Out[1]= a
```

---

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

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

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

The outer ``Catch`` catches ``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)
```

### Possible Issues (2)

``Throw`` requires ``Catch`` :

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

Throw::nocatch: Uncaught Throw[b] returned to top level.

```wl
Out[1]= Hold[Throw[b]]
```

---

Using ``Throw`` can affect the structure of what is returned by a function:

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

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

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

Out[2]= 458330
```

## See Also

* [`Catch`](https://reference.wolfram.com/language/ref/Catch.en.md)
* [`Confirm`](https://reference.wolfram.com/language/ref/Confirm.en.md)
* [`Return`](https://reference.wolfram.com/language/ref/Return.en.md)
* [`Goto`](https://reference.wolfram.com/language/ref/Goto.en.md)
* [`Interrupt`](https://reference.wolfram.com/language/ref/Interrupt.en.md)
* [`Abort`](https://reference.wolfram.com/language/ref/Abort.en.md)
* [`Sow`](https://reference.wolfram.com/language/ref/Sow.en.md)
* [`NestWhile`](https://reference.wolfram.com/language/ref/NestWhile.en.md)
* [`For`](https://reference.wolfram.com/language/ref/For.en.md)
* [`While`](https://reference.wolfram.com/language/ref/While.en.md)
* [`Until`](https://reference.wolfram.com/language/ref/Until.en.md)
* [`AskConfirm`](https://reference.wolfram.com/language/ref/AskConfirm.en.md)

## Tech Notes

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

## 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=Throw)
* [A New Kind of Science](http://www.wolframscience.com/nks/)

## History

* Introduced in 1988 (1.0) \| Updated in 1996 (3.0) ▪ [2020 (12.2)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn122.en.md)