This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Catch

Catch[expr]
returns the argument of the first Throw generated in the evaluation of expr.
Catch
returns value from the first Throw for which form matches tag.
Catch
returns .
  • Catch 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 is re-evaluated every time it is compared to form.
Exit to the enclosing Catch as soon as the Throw is evaluated:
Define a function that can "throw an exception":
The result of the Catch is just what is thrown by Throw:
Use Throw to exit a loop when a criterion is satisfied:
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]=
Catch can catch a Throw from inside essentially any function:
The nearest enclosing Catch catches the Throw:
Catch picks up the first Throw that is evaluated:
Throw need not occur lexically inside Catch:
A function that can throw a number of different exceptions:
A handler for the possible exceptions:
Catch the Throw with tag :
The inner Catch catches the Throw:
The outer Catch catches the Throw:
Keep the tag local:
Find the next prime after :
Find the first power of 17 equal to 1 mod 19:
Stop if an iteration gets too large:
Use Check to throw an exception if a message is generated:
New in 1 | Last modified in 3