Wolfram Research

Function Repository Resource:

ThrowFailure

Source Notebook

Throw a Failure object to the nearest enclosing CatchFailure

Contributed by: Wolfram Staff

ResourceFunction["ThrowFailure"]["msg",args ]

throws a failure object with the tag General, message "msg" and message parameters args to be caught by the nearest enclosing ResourceFunction["CatchFailure"].

ResourceFunction["ThrowFailure"][head::msg,args ]

specifies a message template and tag to use for the failure object.

ResourceFunction["ThrowFailure"][]

throws $Failed.

Details and Options

ResourceFunction["ThrowFailure"] has attribute HoldFirst.
Calling ResourceFunction["ThrowFailure"] without an enclosing CatchFailure prints a message and returns $Aborted.

Examples

Basic Examples

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]=

Possible Issues

Calling ThrowFailure without an enclosing CatchFailure prints a message and returns $Aborted:

In[4]:=
ResourceFunction["ThrowFailure"][]
Out[4]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information