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 .

DetailsDetails

  • 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.

ExamplesExamplesopen allclose all

Basic Examples  (3)Basic Examples  (3)

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

In[1]:=
Click for copyable input
Out[1]=

Define a function that can "throw an exception":

In[1]:=
Click for copyable input

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

In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=

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

In[1]:=
Click for copyable input
Out[1]=
Introduced in 1988
(1.0)
| Updated in 1996
(3.0)