Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Evaluate an expression that returns the result unless messages were generated, in which case it immediately returns a Failure expression
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. |
CheckReturn returns the failure expression immediately, without finishing evaluation:
| In[1]:= |
| Out[1]= |
Check only for specific messages:
| In[2]:= | ![]() |
| Out[2]= |
| In[3]:= | ![]() |
| Out[3]= |
For a single argument, failures give $Failed:
| In[4]:= |
| Out[4]= |
CheckReturn does not detect messages suppressed with an inner Quiet:
| In[5]:= |
| Out[5]= |
However, the operation of CheckReturn is not affected by being inside Quiet:
| In[6]:= |
| Out[6]= |
Use the fourth argument to detect messages that have been suppressed with an inner Quiet:
| In[7]:= |
| Out[7]= |
CheckReturn is similar to FailOnMessage, except that the third argument is a list of messages that should trigger the message:
| In[8]:= |
| Out[8]= |
The third argument of the resource function FailOnMessage specifies a message that should be ignored:
| In[9]:= |
| Out[9]= |
To achieve the same behavior, the fourth argument is needed:
| In[10]:= |
| Out[10]= |
When a Quiet is used to suppress messages, CheckReturn will respect the Quiet:
| In[11]:= |
| Out[11]= |
Check behaves similarly:
| In[12]:= |
| Out[12]= |
In the following, CheckReturn only returns after the second, unquieted division by zero occurs:
| In[13]:= |
| Out[13]= |
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[14]:= |
| Out[14]= |
This work is licensed under a Creative Commons Attribution 4.0 International License