Function Repository Resource:

CheckReturn

Source Notebook

Evaluates an expression and returns the result unless messages were generated, in which case it immediately returns a Failure expression

Contributed by: Carl Woll

ResourceFunction["CheckReturn"][expr]

returns the evaluation of expr unless a message was generated, in which case it immediately returns $Failed.

ResourceFunction["CheckReturn"][expr,failexpr]

evaluates and returns failexpr when a message is generated.

ResourceFunction["CheckReturn"][expr,failexpr,{s1::t1,s2::t2,}]

checks only for the specified messages.

ResourceFunction["CheckReturn"][expr,failexpr,"name"]

checks only for messages in the named message group.

ResourceFunction["CheckReturn"][expr,failexpr,messages,listen]

checks for the messages listen even if they have been quieted during the evaluation of expr.

Details and Options

ResourceFunction["CheckReturn"] has the attribute HoldAll
In ResourceFunction["CheckReturn"][expr,failexpr,"name"], possible named message groups are given by $MessageGroups.
Explicit message names of the form s::t can be mixed with named message groups.
ResourceFunction["CheckReturn"][expr,failexpr,m] does not test for messages that have been switched off using Off, or by uses of Quiet that occur inside it.
Use ResourceFunction["CheckReturn"][expr,failexpr,m,listen] to test for messages that have been quieted due to uses of Quiet that occur inside expr.

Examples

Basic Examples (2) 

CheckReturn returns the failure expression immediately, without finishing evaluation:

In[1]:=
ResourceFunction["CheckReturn"][1/0; Print[1], 2]
Out[1]=

Check only for specific messages:

In[2]:=
ResourceFunction["CheckReturn"][Sin[1, 2]; Print[1]; "Specified message not detected", "Specified message detected", Sin::argx]
Out[2]=
In[3]:=
ResourceFunction["CheckReturn"][1/0; Print[1]; "Specified message not detected", "Specified message detected", Sin::argx]
Out[3]=

Scope (3) 

CheckReturn does not detect messages suppressed with an inner Quiet:

In[4]:=
Check[r = Quiet[1/0]; Print[1]; r, error]
Out[4]=

However, the operation of CheckReturn is not affected by being inside Quiet:

In[5]:=
Quiet[ResourceFunction["CheckReturn"][1/0; Print[1], error]]
Out[5]=

Use the fourth argument to detect messages that have been suppressed with an inner Quiet:

In[6]:=
ResourceFunction["CheckReturn"][Quiet[1/0]; Print[1], error, None, Power::infy]
Out[6]=

Properties and Relations (2) 

CheckReturn is similar to FailOnMessage, except that the third argument is a list of messages that should trigger the message:

In[7]:=
ResourceFunction["CheckReturn"][1/0, $Failed, Power::infy]
Out[7]=

The third argument of the resource function FailOnMessage specifies a message that should be ignored:

In[8]:=
ResourceFunction["FailOnMessage"][1/0, $Failed, Power::infy]
Out[8]=

To achieve the same behavior, the fourth argument is needed:

In[9]:=
ResourceFunction["FailOnMessage"][1/0, $Failed, None, Power::infy]
Out[9]=

When a Quiet is used to suppress messages, CheckReturn will respect the Quiet:

In[10]:=
ResourceFunction["CheckReturn"][Quiet[1/0], $Failed, Power::infy]
Out[10]=

Check behaves similarly:

In[11]:=
Check[Quiet[1/0], $Failed, Power::infy]
Out[11]=

In the following, CheckReturn only returns after the second, unquieted division by zero occurs:

In[12]:=
ResourceFunction["CheckReturn"][Quiet[1/0]; Print[1]; 1/0, $Failed, Power::infy]
Out[12]=

It is not possible to achieve this behavior with the resource function FailOnMessage, as the return occurs during the quieted division by zero (since the Print never occurs):

In[13]:=
ResourceFunction["FailOnMessage"][Quiet[1/0]; Print[1]; 1/0, $Failed, None, Power::infy]
Out[13]=

Publisher

Carl Woll

Version History

  • 1.0.0 – 23 October 2019

Related Resources

License Information