25
$\begingroup$

I have some Mathematica code that analyzes data in a database and uses Message to report when expected data is missing. By default, Mathematica will allow this message to appear only three times before it gets shut off, but I'd like this message to appear as often as the errors do.

The solutions I can think of are

Off[General::stop];

but this kills limits for all error messages, not just this one,

or just to switch to Print, which seems inelegant.

Is there some way to allow this one message to appear more than three times, while leaving behavior unchanged for the rest?

$\endgroup$
1
  • 1
    $\begingroup$ Perhaps Assert would be useful for this? $\endgroup$ Feb 29, 2012 at 19:24

3 Answers 3

10
$\begingroup$

You can do something like this:

resetMessages[symbol_] := 
 With[{mysymbol = symbol}, 
  Unprotect[$MessageList]; $MessageList = 
   DeleteCases[$MessageList, HoldForm[MessageName[mysymbol, _]]]; 
  Protect[$MessageList];]

And you will have to call it after each function...

Sqrt[a, b, c, d]; Exp[a, b]; resetMessages[Exp]; Sqrt[a, b, c, d]; \
Exp[a, b]; resetMessages[Exp]; Sqrt[a, b, c, d]; Exp[a, b]; \
resetMessages[Exp]; Sqrt[a, b, c, d]; Exp[a, b]; resetMessages[Exp]; \
Sqrt[a, b, c, d]; Exp[a, b]; resetMessages[Exp]; Sqrt[a, b, c, d]; \
Exp[a, b]; resetMessages[Exp]; Sqrt[a, b, c, d]; Exp[a, b]; \
resetMessages[Exp];

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

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

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

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

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

General::stop: Further output of Sqrt::argx will be suppressed during this calculation. >>

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

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

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

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

Exp::argx: Exp called with 2 arguments; 1 argument is expected. >>
$\endgroup$
1
  • $\begingroup$ I independently arrived at $MessageList as well, but I used it a little differently. See my answer below. Also, +1. $\endgroup$
    – Mr.Wizard
    Feb 29, 2012 at 19:34
13
$\begingroup$

New Method

FJRA pointed out that my original method will fail in certain cases. Here is what I hope is a more robust approach:

Unprotect[Message, $MessageList]

Message[args___] /; ! TrueQ[$msgClear] :=
 Block[{$msgClear = True},
   $MessageList = DeleteCases[$MessageList, HoldForm[Power::infy]];
   Message[args]
 ]

Now the specified message will print an infinite number of times:

Table[1/0, {7}]

You could use Alternatives in DeleteCases to make multiple messages infinite.


Old Method

I also came up with tinkering with $MessageList as did FJRA, but my application is a little bit different:

Unprotect[$MessageList];

$MessagePrePrint =
  ($MessageList = DeleteCases[$MessageList, HoldForm[Power::infy]]; #) &;
$\endgroup$
5
  • 2
    $\begingroup$ This is more elegant, the only problem I see is that $MessagePrePrint is only used when the message has arguments like 1, 2. If the message is only a string it doesn't call $MessagePrePrint. But in general it avoids the "manual" reset, which is cleaner. $\endgroup$
    – FJRA
    Feb 29, 2012 at 20:02
  • $\begingroup$ @FJRA I didn't realize that; thank you. Please see up updated answer and tell me if you can see any specific problems with my new method. $\endgroup$
    – Mr.Wizard
    Feb 29, 2012 at 20:19
  • $\begingroup$ I think that's the best way to clear the message list, because calling extra functions is not needed. $\endgroup$
    – FJRA
    Feb 29, 2012 at 20:30
  • 1
    $\begingroup$ I wanted to do this, but finally decided against it: it will break some unit testing tools which rely on $MessageList to catch messages. $\endgroup$
    – Szabolcs
    Sep 13, 2015 at 11:16
  • $\begingroup$ @Szabolcs I can imagine that being a problem. I wish I knew another way. p.s. thanks for the corrections to my "MaxMessageCount" question. $\endgroup$
    – Mr.Wizard
    Sep 14, 2015 at 9:07
6
$\begingroup$

Why don't you use Check. You could combine it with Reap/Sow or Throw/Catch. Here's an example that counts divisions by zero.

SeedRandom[1];
list = RandomInteger[{-2, 2}, {100}];
Quiet[results = Reap[Table[Check[1/x, Sow["bad news"]],
  {x, list}]]];
Length[results[[2, 1]]]

Of course, since the second argument of Check can be anything, you could arrange for it to alert you any number of ways.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.