PRODUCTS
Products Overview
Mathematica
Mathematica Student Edition
Mathematica Home Edition
Wolfram
CDF Player
(free download)
Computable Document Format (CDF)
web
Mathematica
grid
Mathematica
Wolfram
Workbench
Wolfram
SystemModeler
Wolfram
Finance Platform
Mathematica
Add-Ons
Wolfram|Alpha Products
SOLUTIONS
Solutions Overview
Engineering
Aerospace Engineering & Defense
Chemical Engineering
Control Systems
Electrical Engineering
Image Processing
Industrial Engineering
Materials Science
Mechanical Engineering
Operations Research
Optics
Petroleum Engineering
Biotechnology & Medicine
Bioinformatics
Medical Imaging
Finance, Statistics & Business Analysis
Actuarial Sciences
Data Analysis & Mining
Econometrics
Economics
Financial Engineering & Mathematics
Financial Risk Management
Statistics
Software Engineering & Content Delivery
Authoring & Publishing
Interface Development
Software Engineering
Web Development
Science
Astronomy
Biological Sciences
Chemistry
Environmental Sciences
Geosciences
Social & Behavioral Sciences
Design, Arts & Entertainment
Game Design, Special Effects & Generative Art
Education
STEM Education Initiative
Higher Education
Community & Technical College Education
Primary & Secondary Education
Students
Technology
Computable Document Format (CDF)
High-Performance & Parallel Computing (HPC)
See Also: Technology Guide
PURCHASE
Online Store
Other Ways to Buy
Volume & Site Licensing
Contact Sales
Software
Service
Upgrades
Training
Books
Merchandise
SUPPORT
Support Overview
Mathematica
Documentation
Knowledge Base
Learning Center
Technical Services
Community & Forums
Training
Does My Site Have a License?
Wolfram User Portal
COMPANY
About Wolfram Research
News
Events
Wolfram Blog
Partnerships
Employment Opportunities
History of
Mathematica
Stephen Wolfram's Home Page
Contact Us
OUR SITES
All Sites
Wolfram|Alpha
Demonstrations Project
MathWorld
Integrator
Wolfram Functions Site
Mathematica Journal
Wolfram Media
Wolfram
Tones
Wolfram Science
Stephen Wolfram
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE
DOCUMENTATION CENTER
FOR THE LATEST INFORMATION.
DOCUMENTATION CENTER SEARCH
New to
Mathematica
?
Find your learning path
»
Mathematica
>
Core Language
>
Procedural Programming
>
Flow Control
>
Catch
>
BUILT-IN MATHEMATICA SYMBOL
Loops and Control Structures
Tutorials »
|
Throw
Check
CheckAbort
Reap
See Also »
|
Flow Control
Looping Constructs
Procedural Programming
Scoping Constructs
More About »
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
.
MORE INFORMATION
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
.
EXAMPLES
CLOSE ALL
Basic Examples
(3)
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]:=
Out[1]=
Define a function that can "throw an exception":
In[1]:=
The result of the
Catch
is just what is thrown by
Throw
:
In[2]:=
Out[2]=
In[3]:=
Out[3]=
Use
Throw
to exit a loop when a criterion is satisfied:
In[1]:=
Out[1]=
Scope
(5)
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:
Generalizations & Extensions
(3)
Catch the
Throw
with tag
:
The inner
Catch
catches the
Throw
:
The outer
Catch
catches the
Throw
:
Keep the tag local:
Applications
(3)
Find the next prime after
:
Find the first power of 17 equal to 1 mod 19:
Stop if an iteration gets too large:
Properties & Relations
(1)
Use
Check
to throw an exception if a message is generated:
SEE ALSO
Throw
Check
CheckAbort
Reap
TUTORIALS
Loops and Control Structures
MORE ABOUT
Flow Control
Looping Constructs
Procedural Programming
Scoping Constructs
RELATED LINKS
NKS|Online
(
A New Kind of Science
)
New in 1 | Last modified in 3