WOLFRAM

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 reevaluated every time it is compared to form.

Examples

open allclose all

Basic Examples  (3)Summary of the most common use cases

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

Out[1]=1

Define a function that can "throw an exception":

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

Out[2]=2
Out[3]=3

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

Out[1]=1

Scope  (5)Survey of the scope of standard use cases

Catch can catch a Throw from inside essentially any function:

Out[1]=1
Out[2]=2
Out[3]=3

The nearest enclosing Catch catches the Throw:

Out[1]=1

Catch picks up the first Throw that is evaluated:

Out[1]=1
Out[2]=2

Throw need not occur lexically inside Catch:

Out[2]=2

A function that can throw a number of different exceptions:

A handler for the possible exceptions:

Out[3]=3

Generalizations & Extensions  (3)Generalized and extended use cases

Catch the Throw with tag u:

Out[1]=1

The inner Catch catches the Throw:

Out[1]=1

The outer Catch catches the Throw:

Out[2]=2

Keep the tag local:

Out[1]=1

Applications  (3)Sample problems that can be solved with this function

Find the next prime after 1010:

Out[1]=1

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

Out[1]=1

Stop if an iteration gets too large:

Out[1]=1
Out[2]=2

Properties & Relations  (1)Properties of the function, and connections to other functions

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

Out[1]=1
Out[2]=2
Wolfram Research (1988), Catch, Wolfram Language function, https://reference.wolfram.com/language/ref/Catch.html (updated 1996).
Wolfram Research (1988), Catch, Wolfram Language function, https://reference.wolfram.com/language/ref/Catch.html (updated 1996).

Text

Wolfram Research (1988), Catch, Wolfram Language function, https://reference.wolfram.com/language/ref/Catch.html (updated 1996).

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.

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

Wolfram Language. (1988). Catch. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Catch.html

BibTeX

@misc{reference.wolfram_2025_catch, author="Wolfram Research", title="{Catch}", year="1996", howpublished="\url{https://reference.wolfram.com/language/ref/Catch.html}", note=[Accessed: 16-April-2025 ]}

@misc{reference.wolfram_2025_catch, author="Wolfram Research", title="{Catch}", year="1996", howpublished="\url{https://reference.wolfram.com/language/ref/Catch.html}", note=[Accessed: 16-April-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_catch, organization={Wolfram Research}, title={Catch}, year={1996}, url={https://reference.wolfram.com/language/ref/Catch.html}, note=[Accessed: 16-April-2025 ]}

@online{reference.wolfram_2025_catch, organization={Wolfram Research}, title={Catch}, year={1996}, url={https://reference.wolfram.com/language/ref/Catch.html}, note=[Accessed: 16-April-2025 ]}