Message

In addition to what I present here you might also want to study (PackageDesignTutorial.nb) posted at

http://library.wolfram.com/infocenter/Conferences/184

In that notebook you will find a section on error handling where good use of messages is covered. A non-trivial application of messages is given below, but first I define a function in the next cell that computes the magnitude of a vector is defined and the function is used to find the magnitude of the vector {2, 3, 4}.

The next cell demonstrates that when Mathematica is given invalid input it normally posts a message and returns the expression that is undefined.

We would like our Mag function to behave like a built in function when given

an invalid argument. The next cell defines a message for this purpose.

We could use the definition Mag[expr_]:=Message[mag::vect,expr]. That would

cause Mag[expr] with an invalid argument to evaluate to a message and the

input expression would not be returned. But we want Mag to work like a

built-in function so we have it post a message, and return expression passed

to Mag. This can be done by putting the message in a condition as in the

next line. When this is done the condition will never evaluate to True, so

the right side of the definition can be almost anything.

Now when Mag is given an argument that isn't a vector it prints a message and returns the input expression since no definition matched. Here the argument given to Mag was which evaluated to {{1,19},{2,3}}. None of the Mag definitions apply, so is returned.

Mathematica has an undocumented function called ArgumentCountQ which can be useful when making definitions for functions that post a message when the function is given an incorrect number of arguments. An explanation of this feature can be found by looking up ArgumentCountQ at http://support.wolfram.com/Kernel/Symbols/

Checking for non-options

We can also account for the possibility that one or more of several arguments

are not options when only options are allowed in these positions. This is

what I do with Func defined below. At the begining of the next cell I clear

values assigned to all global variables.

Next we see Func[__] evaluates whether there are options or not.

When one or more argument beyond the first in Func[__] are not options we get

messages as below, and the original expression is returned.

How to tell if a message is On or Off

The direct answer to this problem is the following:

If Head[symbol::tag] === Off

then (the message is Off)

else (the message is On)

If a message is on and the message is not one of the general messages (which

are explained in the previous sub-section), then the message has the head

String. That is why the next cell returns String.

On the other hand if a message is on and the message is one of the general messages, then the message has the head MessageName. That is why the next cell returns MessageName. As explained above Mathematica defines a message (General::argx) that can be used as a message for any function, but the message (Show::gtype) is only associated with Show unless you make assignments that associate it with other symbols.

The next cell is typical for what (symbol::tag) evaluates to when the message

is Off, and the message is not one of the general messages.

The next cell shows what (symbol::tag) evaluates to when the message is Off,

and the message is one of the general messages.

The next input turns both messages back on.

Created by Mathematica (May 16, 2004)