Function Repository Resource:

CatchFailure

Source Notebook

Catches any Failure object thrown by an enclosed ThrowFailure evaluation

Contributed by: Wolfram Staff (Wolfram Research)

ResourceFunction["CatchFailure"][body]

evaluates body, but returns a Failure[] expression if a ThrowFailure[] occurred during evaluation.

ResourceFunction["CatchFailure"][head,body]

explicitly uses head as the tag of the returned a Failure[] object.

Details and Options

ResourceFunction["CatchFailure"] has attribute HoldAll.

Examples

Basic Examples (4) 

Throw and catch a generic Failure:

In[1]:=
ResourceFunction["CatchFailure"][
 ResourceFunction["ThrowFailure"]["There was an error!", myFunc]; Print["goodbye"]]
Out[1]=

Throw and catch a Failure using the specified message template and tag:

In[2]:=
ResourceFunction["CatchFailure"][
 ResourceFunction["ThrowFailure"][myFunc::argx, Pi, 2.9]; Print["goodbye"]]
Out[2]=

Throw and catch $Failed:

In[3]:=
ResourceFunction["CatchFailure"][ResourceFunction["ThrowFailure"][]; Print["goodbye"]]
Out[3]=

Throw and catch a Failure whose head is specified in the first argument of CatchFailure:

In[4]:=
ResourceFunction["CatchFailure"][yourFunc, ThrowFailure["argx", Pi, 2.9]; Print["goodbye"]]
Out[4]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 20 February 2019

Related Resources

License Information