Catch
Examples
open all close allBasic Examples (3)
Exit to the enclosing Catch as soon as the Throw is evaluated:
Catch[a;b;Throw[c];d;e]Define a function that can "throw an exception":
f[x_] := If[x > 10, Throw[overflow], x!]The result of the Catch is just what is thrown by Throw:
Catch[f[2] + f[11]]Catch[f[2] + f[3]]Use Throw to exit a loop when a criterion is satisfied:
Catch[Do[If[i! > 10 ^ 10, Throw[i]], {i, 100}]]Scope (5)
Catch can catch a Throw from inside essentially any function:
Catch[If[# < 0, Throw[#]]& /@ {1, 2, 0, -1, 5, 6}]Catch[{a, Throw[b], c}]Catch[a ^ 2 + b ^ 2 + c ^ 2 /. b :> Throw[bbb]]The nearest enclosing Catch catches the Throw:
Catch[{Catch[{a, Throw[b], c}], d, e}]Catch picks up the first Throw that is evaluated:
Catch[{Throw[a], Throw[b], Throw[c]}]Catch[Throw /@ {a, b, c}]Throw need not occur lexically inside Catch:
f[x_] := (If[x < 0, Throw["negative"]];Sqrt[x])Catch[Sum[f[i], {i, 5, -5, -1}]]A function that can throw a number of different exceptions:
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:
ff[x_] :=
Catch[f[x], error[_], Function[{value, tag}, tag /. {error[negative] :> Indeterminate, error[zero] :> Infinity, _ :> Throw[value, tag]}]]ff /@ {-1, 0, 1}Generalizations & Extensions (3)
Catch the Throw with tag u:
Catch[Throw[a, u], u]The inner Catch catches the Throw:
Catch[f[Catch[Throw[a, u], u]], v]The outer Catch catches the Throw:
Catch[f[Catch[Throw[a, u], v]], u]Module[{u}, Catch[Throw[a, u], u]]Applications (3)
Find the next prime after 1010:
Catch[Do[If[PrimeQ[i], Throw[i]], {i, 10 ^ 10, 10 ^ 10 + 1000}]]Find the first power of 17 equal to 1 mod 19:
Catch[Do[If[Mod[17 ^ i, 19] == 1, Throw[i]], {i, 19}]]Stop if an iteration gets too large:
NestList[# ^ 2 + 1&, 2, 6]Catch[NestList[If[# > 1000, Throw[#], # ^ 2 + 1]&, 2, 6]]Properties & Relations (1)
Use Check to throw an exception if a message is generated:
Catch[2 + Check[1 / (1 + 1 / 0), Throw[error]]]Catch[2 + Check[1 / (1 + 1 / 2), Throw[error]]]Tech Notes
Related Guides
History
Introduced in 1988 (1.0) | Updated in 1996 (3.0)
Text
Wolfram Research (1988), Catch, Wolfram Language function, https://reference.wolfram.com/language/ref/Catch.html (updated 1996).
CMS
Wolfram Language. 1988. "Catch." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 1996. https://reference.wolfram.com/language/ref/Catch.html.
APA
Wolfram Language. (1988). Catch. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Catch.html
BibTeX
@misc{reference.wolfram_2026_catch, author="Wolfram Research", title="{Catch}", year="1996", howpublished="\url{https://reference.wolfram.com/language/ref/Catch.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_catch, organization={Wolfram Research}, title={Catch}, year={1996}, url={https://reference.wolfram.com/language/ref/Catch.html}, note=[Accessed: 13-June-2026]}