Messages

The Wolfram Language has a general mechanism for handling messages generated during computations. Many builtin Wolfram Language functions use this mechanism to produce error and warning messages. You can also use the mechanism for messages associated with functions you write.

The basic idea is that every message has a definite name, of the form . You can use this name to refer to the message. (The object has head MessageName.)

Quiet[expr]evaluate expr without printing any messages
Quiet[expr,{s1::tag,s2::tag,}]evaluate expr without printing the specified messages
Off[s::tag]switch off a message, so it is not printed
On[s::tag]switch on a message

Controlling the printing of messages.

As discussed in "Warnings and Messages", you can use Quiet to control the printing of particular messages during an evaluation. Most messages associated with builtin functions are switched on by default. If you want to suppress a message permanently, you can use Off.

This prints a warning message. Also, the front end highlights the extra argument in red.
In[1]:=
Click for copyable input
Out[1]=
This suppresses the warning message.
In[2]:=
Click for copyable input
Out[2]=
The message reappears with the next evaluation.
In[3]:=
Click for copyable input
Out[3]=

You can use On and Off to make global changes to the printing of particular messages. You can use Off to switch off a message if you never want to see it.

You can switch off the message like this.
In[4]:=
Click for copyable input
Now no warning message is produced.
In[5]:=
Click for copyable input
Out[5]=

Although most messages associated with builtin functions are switched on by default, there are some which are switched off by default, and which you will see only if you explicitly switch them on. An example is the message General::newsym, discussed in "Intercepting the Creation of New Symbols", which tells you every time a new symbol is created.

s::taggive the text of a message
s::tag=stringset the text of a message
Messages[s]show all messages associated with s

Manipulating messages.

The text of a message with the name is stored simply as the value of , associated with the symbol s. You can therefore see the text of a message simply by asking for . You can set the text by assigning a value to .

If you give LinearSolve a singular matrix, it prints a warning message.
In[6]:=
Click for copyable input
Out[6]=
Here is the text of the message.
In[7]:=
Click for copyable input
Out[7]=
This redefines the message.
In[8]:=
Click for copyable input
Out[8]=
Now the new form will be used.
In[9]:=
Click for copyable input
Out[9]=

Messages are always stored as strings suitable for use with StringForm. When the message is printed, the appropriate expressions are "spliced" into it. The expressions are wrapped with HoldForm to prevent evaluation. In addition, any function that is assigned as the value of the global variable $MessagePrePrint is applied to the resulting expressions before they are given to StringForm. The default for $MessagePrePrint uses Short for text formatting and a combination of Short and Shallow for typesetting.

Most messages are associated directly with the functions that generate them. There are, however, some "general" messages, which can be produced by a variety of functions.

If you give the wrong number of arguments to a function F, the Wolfram Language will warn you by printing a message such as F::argx. If the Wolfram Language cannot find a message named F::argx, it will use the text of the "general" message General::argx instead. You can use Off[F::argx] to switch off the argument count message specifically for the function F. You can also use Off[General::argx] to switch off all messages that use the text of the general message.

The Wolfram Language prints a message if you give the wrong number of arguments to a builtin function.
In[10]:=
Click for copyable input
Out[10]=
This argument count message is a general one, used by many different functions.
In[11]:=
Click for copyable input
Out[11]=

Quietening Sqrt::argx suppresses the message issued by the Sqrt function but not by other functions.

This prints the Sin::argx message but not Sqrt::argx.
In[12]:=
Click for copyable input
Out[12]=

Quietening General::argx suppresses all messages that use General::argx.

Here, no messages are printed.
In[13]:=
Click for copyable input
Out[13]=

If message is not using General::tag, suppressing General::tag will not suppress . For example, turning off the General::targ message will not turn off the Entropy::targ message, because those two messages are not one and the same.

Entropy::targ is not the same as General::targ.
In[14]:=
Click for copyable input
Out[14]=
IdentityMatrix::targ is the same message as General::targ.
In[15]:=
Click for copyable input
Out[15]=
Entropy::targ message is still printed inside of Quiet.
In[16]:=
Click for copyable input
Out[16]=

If something goes very wrong with a calculation you are doing, it is common to find that the same warning message is generated over and over again. This is usually more confusing than useful. As a result, the Wolfram Language keeps track of all messages that are produced during a particular calculation, and stops printing a particular message if it comes up more than three times. Whenever this happens, the Wolfram Language prints the message General::stop to let you know. If you really want to see all the messages that the Wolfram Language tries to print, you can do this by switching off General::stop.

$MessageLista list of the messages produced during a particular computation
MessageList[n]a list of the messages produced during the processing of the n^(th) input line in a Wolfram Language session

Finding out what messages were produced during a computation.

In every computation you do, the Wolfram Language maintains a list $MessageList of all the messages that are produced. In a standard Wolfram Language session, this list is cleared after each line of output is generated. However, during a computation, you can access the list. In addition, when the n^(th) output line in a session is generated, the value of $MessageList is assigned to MessageList[n].

This returns $MessageList, which gives a list of the messages produced.
In[17]:=
Click for copyable input
Out[17]=
The message names are wrapped in HoldForm to stop them from evaluating.

In writing programs, it is often important to be able to check automatically whether any messages were generated during a particular calculation. If messages were generated, say as a consequence of producing indeterminate numerical results, then the result of the calculation may be meaningless.

Check[expr,failexpr]if no messages are generated during the evaluation of expr, then return expr; otherwise return failexpr
Check[expr,failexpr,s1::t1,s2::t2,]check only for the messages

Checking for warning messages.

Evaluating produces no messages, so the result of the evaluation is returned.
In[19]:=
Click for copyable input
Out[19]=
Evaluating produces a message, so the second argument of Check is returned.
In[20]:=
Click for copyable input
Out[20]=

Check[expr,failexpr] tests for all messages that are actually printed out. It does not test for messages whose output has been suppressed using Off.

In some cases you may want to test only for a specific set of messages, say ones associated with numerical overflow. You can do this by explicitly telling Check the names of the messages you want to look for.

The message generated by Sin[1,2] is ignored by Check, since it is not the one specified.
In[21]:=
Click for copyable input
Out[21]=
Message[s::tag]print a message
Message[s::tag,expr1,]print a message, with the spliced into its string form

Generating messages.

By using the function Message, you can mimic all aspects of the way in which builtin Wolfram Language functions generate messages. You can for example switch on and off messages using On and Off, and Message will automatically look for General::tag if it does not find the specific message .

This defines the text of a message associated with .
In[22]:=
Click for copyable input
Out[22]=
Here is the function .
In[23]:=
Click for copyable input
When the argument of is greater than 10, the message is generated.
In[24]:=
Click for copyable input
Out[24]=
This switches off the message.
In[25]:=
Click for copyable input
Now the message is no longer generated.
In[26]:=
Click for copyable input
Out[26]=

When you call Message, it first tries to find a message with the explicit name you have specified. If this fails, it tries to find a message with the appropriate tag associated with the symbol General. If this too fails, then the Wolfram Language takes any function you have defined as the value of the global variable $NewMessage, and applies this function to the symbol and tag of the message you have requested.

By setting up the value of $NewMessage appropriately, you can, for example, get the Wolfram Language to read in the text of a message from a file when that message is first needed.