Function Repository Resource:

LimitedMessage

Source Notebook

Issue a message a customized number of times per evaluation

Contributed by: Bob Sandheinrich

ResourceFunction["LimitedMessage"][n,symbol::tag,e1,e2,]

issues Message[symbol::tag,ei,e2,] up to n times per evaluation.

ResourceFunction["LimitedMessage"][{n,"NoStopMessage"},symbol::tag,e1,e2,]

issues the message up to n times per evaluation and stops without a General::stop message.

Details

By default, Message issues a given message three times per evaluation before stopping with a General::stop message. ResourceFunction["LimitedMessage"] allows you to set the number of times to a value different than three.
ResourceFunction["LimitedMessage"][{n,False},] is a convenient alternative to ResourceFunction["LimitedMessage"][{n,"NoStopMessage"},] for those of us that can never remember magic string values.

Examples

Basic Examples (1) 

Run an evaluation that issues a message five times, but limit it to only issue twice:

In[1]:=
Do[ResourceFunction["LimitedMessage"][2, f::argx, 1, 2], 5]

Scope (3) 

Define a function with a message that can be issued up to ten times per input evaluation:

In[2]:=
rsqrt::nnarg = "The argument `1` is not greater than or equal to zero.";
rsqrt[x_] := If[TrueQ[x >= 0], Sqrt[x], ResourceFunction["LimitedMessage"][10, rsqrt::nnarg, x];]

Use the function on data that frequently generates a message:

In[3]:=
rsqrt /@ RandomReal[{-10, 10}, 30]
Out[3]=

Prevent a message from being issued at all by limiting the count to zero:

In[4]:=
Do[ResourceFunction["LimitedMessage"][0, f::argx, 1, 2], 5]

Turn off the General::stop message:

In[5]:=
Do[ResourceFunction["LimitedMessage"][{2, "NoStopMessage"}, f::argx, 1, 2], 5]

Alternatively use True or False to control the stop message:

In[6]:=
Do[ResourceFunction["LimitedMessage"][{2, False}, f::argx, 1, 2], 5]
In[7]:=
Do[ResourceFunction["LimitedMessage"][{2, True}, f::argx, 1, 2], 5]

Properties and Relations (2) 

By default, Message issues messages three times:

In[8]:=
Do[Message[f::argx, 1, 2], 5]

LimitedMessage[3,] is equivalent:

In[9]:=
Do[ResourceFunction["LimitedMessage"][3, f::argx, 1, 2], 5]

Use Off to allow all messages:

In[10]:=
Off[General::stop]
In[11]:=
Do[Message[f::argx, 1, 2], 5]
In[12]:=
On[General::stop]

LimitedMessage[Infinity,] is equivalent:

In[13]:=
Do[ResourceFunction["LimitedMessage"][Infinity, f::argx, 1, 2], 5]

Publisher

Bob

Version History

  • 1.0.0 – 19 April 2022

Related Resources

Author Notes

The "NoStopMessage" syntax to prevent the stop message is a pretty ugly design. It would be nice to implement it as an option, but Message is variable length and accepts any expression, including Rule as an argument, so there would be an ambiguity about whether the rule here is an option or a message template value:

In[1]:=
LimitedMessage[n, tag, ShowStopMessage -> False]

License Information