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

Throw

Throw[value]
stops evaluation and returns value as the value of the nearest enclosing Catch.
Throw
is caught only by Catch where form is a pattern that matches tag.
  • tag can be any expression.
  • tag in Throw is re-evaluated every time it is compared to form in Catch.
  • An error is generated and an unevaluated Throw is returned if there is no appropriate enclosing Catch to catch the Throw.
Exit to the enclosing Catch as soon as Throw is evaluated:
Define a function that can "throw an exception":
The result of 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 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 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]=
Throw works inside essentially any function:
Use Throw to throw any type of expression:
Throw is caught by the nearest enclosing Catch:
As soon as the first Throw is evaluated, it exits to the enclosing Catch:
Catch Throw with tag :
The inner Catch catches Throw:
The outer Catch catches 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:
Throw requires Catch:
Using Throw can affect the structure of what is returned by a function:
New in 1 | Last modified in 3