Wolfram Research

MessagedQ

Contributed by: Richard Hennigan (Wolfram Research)

Source Notebook

Test if a message is issued during an evaluation

ResourceFunction["MessagedQ"][expr]

returns True if messages were generated during evaluation of expr and False otherwise.

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

only tests if any of the si::ti were generated.

ResourceFunction["MessagedQ"][expr,"name"]

only tests if messages in the named message group were generated.

Details and Options

ResourceFunction["MessagedQ"] has the attribute HoldAll.
ResourceFunction["MessagedQ"] does not test for messages that have been switched off using Off or by uses of Quiet that occur inside it.
The operation of ResourceFunction["MessagedQ"] is not affected by being enclosed inside Quiet.
In ResourceFunction["MessagedQ"][expr,"name"], possible named message groups are given by $MessageGroups.
Explicit message names of the form s::t can be mixed with named message groups.

Examples

Basic Examples

Test if a message is generated:

In[1]:=
ResourceFunction["MessagedQ"][1^0]
Out[1]=
In[2]:=
ResourceFunction["MessagedQ"][0^0]
Out[2]=

Only test for specific messages:

In[3]:=
ResourceFunction["MessagedQ"][Sin[0^0], Sin::argx]
Out[3]=
In[4]:=
ResourceFunction["MessagedQ"][Sin[x, y], Sin::argx]
Out[4]=

Suppress messages with Quiet:

In[5]:=
ResourceFunction["MessagedQ"][Quiet[First[0]]]
Out[5]=

Enclosing MessagedQ[] with Quiet will not affect the inner results:

In[6]:=
Quiet[ResourceFunction["MessagedQ"][First[0]]]
Out[6]=

Properties and Relations

MessagedQ is similar to Check, but only tests whether a message was generated:

In[7]:=
Table[ResourceFunction["MessagedQ"][1/n], {n, -2, 2}]
Out[7]=
In[8]:=
Table[Check[1/n, $error], {n, -2, 2}]
Out[8]=

This behavior can also be obtained with Check:

In[9]:=
Table[Check[1/n, $error] === $error, {n, -2, 2}]
Out[9]=

Similarly, the behavior of Check can be obtained with MessagedQ:

In[10]:=
Table[If[ResourceFunction["MessagedQ"][result = 1/n], $error, result], {n, -2, 2}]
Out[10]=

Resource History

See Also