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}.

Mag[v_ ? VectorQ] := Plus @@ (v . v)^(1/2) <br /> Mag[{2, 3, 4}]

29^(1/2)

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

Sin[π/2, 0]

Sin[π/2, 0]


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.

Mag :: vect = "Mag is only defined for vectors.  The expression `1` \nis not a vector." ;


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.

Mag[expr_]/;Message[Mag :: vect, expr] := "Never get here."

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  {{1, 96/4 - 5}, {2, 3}} which evaluated to {{1,19},{2,3}}.  None of the Mag definitions apply, so   Mag[{{1, 19}, {2, 3}}]  is returned.

Mag[{{1, 96/4 - 5}, {2, 3}}]

Mag[{{1, 19}, {2, 3}}]

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.

ClearAll["Global`*"] ; (* Next I make Func do something when the arguments g ... eyond the first \nargument in Func. Only options are allowed beyond the first \nargument." ;


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

Func[x + y]  Func[a + b, opt1val1, opt2val2]

{x + y}

{a + b}


When one or more argument beyond the first in Func[__] are not options we get
messages as below, and the original expression is returned.

Func[a + b, opt1val1, opt, opt2val2]

Func[a + b, opt1val1, opt, opt2val2]

Func[a + b, opt1val1, opt, opt2val2, opt3]

Func[a + b, opt1val1, opt, opt2val2, opt3]

General Messages

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[Show :: gtype] ;  Head[Show :: gtype]

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.

On[Sin :: argx] ;  Head[Sin :: argx]

MessageName


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.

Off[Show :: gtype] ;  Show :: gtype

$Off[`1` is not a type of graphics.]


The next cell shows what (symbol::tag) evaluates to when the message is Off,
and the message is one of the general messages.

Off[Sin :: argx] ;  Sin :: argx

$Off[]

The next input turns both messages back on.

On[Show :: gtype, Sin :: argx]


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page