Wolfram Research

Function Repository Resource:

CatchAll

Source Notebook

Get $Aborted for uncaught Throw or Abort in an evaluation

Contributed by: Taliesin Beynon

ResourceFunction["CatchAll"][expr]

returns $Aborted if any uncaught Throw or Abort occurs during the evaluation of expr.

ResourceFunction["CatchAll"][expr, result]

evaluates result in the case of an abort.

Details and Options

When a Throw is caught, a $ThrownValue[thrownarg] is generated.
Works with expressions without an explicit Catch.
CatchAll has the attribute HoldAllComplete.

Examples

Basic Examples

Catch $Aborted:

In[1]:=
ResourceFunction["CatchAll"][TimeConstrained[Pause[2], 1]]
Out[1]=

When a Throw is caught, a $ThrownValue is generated:

In[2]:=
ResourceFunction["CatchAll"][{a, Throw[b], c}]
Out[2]=

Catch an overflow evaluating an expression:

In[3]:=
ResourceFunction["CatchAll"][{a, Throw[b], c}, Print["exception!"]]

Define a function:

In[4]:=
f[x_] := If[x == 0, Throw[divisionbyzero], 1/x]
In[5]:=
ResourceFunction["CatchAll"][f[0]]
Out[5]=

Scope

Works with any expression:

In[6]:=
ResourceFunction[
 "CatchAll"][{1, h[r], Style["x", 5]} /. s_String :> Throw[s]]
Out[6]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information