This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Textual Input and Output  /

2.8.21 Messages

Mathematica has a general mechanism for handling messages generated during computations. Many built-in Mathematica 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 symbol::tag. You can use this name to refer to the message. (The object symbol::tag has head MessageName.)


Controlling the printing of messages.

As discussed in Section 1.3.11, you can use On and Off to control the printing of particular messages. Most messages associated with built-in functions are switched on by default. You can use Off to switch them off if you do not want to see them.

  • This prints a warning message.
  • In[1]:= Log[a, b, c]

    Log::argt: Log called with 3 arguments; 1 or 2 arguments are expected.

    Out[1]=

  • You can switch off the message like this.
  • In[2]:= Off[Log::argt]

  • Now no warning message is produced.
  • In[3]:= Log[a, b, c]

    Out[3]=

    Although most messages associated with built-in 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 Section 2.6.13, which tells you every time a new symbol is created.


    Manipulating messages.

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

  • If you give LinearSolve a singular matrix, it prints a warning message.
  • In[4]:= LinearSolve[{{1, 1}, {2, 2}}, {3, 5}]

    LinearSolve::nosol: Linear equation encountered which has no solution.

    Out[4]=

  • Here is the text of the message.
  • In[5]:= LinearSolve::nosol

    Out[5]=

  • This redefines the message.
  • In[6]:= LinearSolve::nosol = "Matrix encountered is not invertible."

    Out[6]=

  • Now the new form will be used.
  • In[7]:= LinearSolve[{{1, 1}, {2, 2}}, {3, 5}]

    LinearSolve::nosol: Matrix encountered is not invertible.

    Out[7]=

    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 is Short.
    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, Mathematica will warn you by printing a message such as F::argx. If Mathematica 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.

  • Mathematica prints a message if you give the wrong number of arguments to a built-in function.
  • In[8]:= Sqrt[a, b]

    Sqrt::argx: Sqrt called with 2 arguments; 1 argument is expected.

    Out[8]=

  • This argument count message is a general one, used by many different functions.
  • In[9]:= General::argx

    Out[9]=

    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, Mathematica 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, Mathematica prints the message General::stop to let you know. If you really want to see all the messages that Mathematica tries to print, you can do this by switching off General::stop.


    Finding out what messages were produced during a computation.




    In every computation you do, Mathematica maintains a list $MessageList of all the messages that are produced. In a standard Mathematica 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 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[10]:= Sqrt[a, b, c]; Exp[a, b]; $MessageList

    Sqrt::argx: Sqrt called with 3 arguments; 1 argument is expected.

    Exp::argx: Exp called with 2 arguments; 1 argument is expected.

    Out[10]=

  • The message names are wrapped in HoldForm to stop them from evaluating.
  • In[11]:= InputForm[%]

    Out[11]//InputForm=

    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.


    Checking for warning messages.

  • Evaluating 1^0 produces no messages, so the result of the evaluation is returned.
  • In[12]:= Check[1^0, err]

    Out[12]=

  • Evaluating 0^0 produces a message, so the second argument of Check is returned.
  • In[13]:= Check[0^0, err]

    0
    Power::indet: Indeterminate expression 0 encountered.

    Out[13]=

    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[14]:= Check[Sin[1, 2], err, General::ind]

    Sin::argx: Sin called with 2 arguments; 1 argument is expected.

    Out[14]=


    Generating messages.

    By using the function Message, you can mimic all aspects of the way in which built-in Mathematica 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 s::tag.

  • This defines the text of a message associated with f.
  • In[15]:= f::overflow = "Factorial argument `1` too large."

    Out[15]=

  • Here is the function f.
  • In[16]:= f[x_] :=
    If[x > 10,
    (Message[f::overflow, x]; Infinity), x!]

  • When the argument of f is greater than 10, the message is generated.
  • In[17]:= f[20]

    f::overflow: Factorial argument 20 too large.

    Out[17]=

  • This switches off the message.
  • In[18]:= Off[f::overflow]

  • Now the message is no longer generated.
  • In[19]:= f[20]

    Out[19]=

    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 Mathematica 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 Mathematica to read in the text of a message from a file when that message is first needed.