2

I have a variety of possible errors in each iteration of a for loop.

If, in one iteration of the loop, there are more than 3 error messages of the same kind, I am happy that the rest are suppressed.

But if errors occur in the next iteration, I need them to be shown.

I tried turning on all error messages after each iteration using On[]; but this turns on a lot of messages which are off by default, which litter my output with red messages that are irrelevant to what I'm doing.

How do I turn on only those error messages which were suppressed? Is there a way to do this without manually catching each suppressed error? Maybe something like On[default], or maybe by previously saving the set of errors which are on, and then resetting back to that state.

doublefelix
  • 291
  • 1
  • 11
  • I think what you are looking for is Off[General::stop], that is switch off the message that is shown after the 3 consecutive messages. – Albert Retey Mar 02 '20 at 19:21
  • That seems like it would just stop telling me that messages are being suppressed. But the messages would continue to be suppressed. So it's not really what I am looking for, unless I'm wrong here. – doublefelix Mar 02 '20 at 20:42
  • yes it seems like that, but actually switching that message Off will cause the repeating messages to not be suppressed. Check the documentation ref/message/General/stop for more details. Or just try it out. If you search this site for "General::stop" you will find several questions and answers, some of them might be considered duplicates to your question... – Albert Retey Mar 03 '20 at 06:40
  • If you want more fine grained control you could manipulate $MessageList as described here – Albert Retey Mar 03 '20 at 06:45

1 Answers1

1

Steeling from this answer this should do what you are after:

resetMessages[] := (
 Unprotect[$MessageList];
 $MessageList = {};
 Protect[$MessageList];
);

You would use it like this:

Do[
  Print[i]; 
  resetMessages[];
  Do[1/0, {5}],
  {i, 3}
]

There will now be shown three messages but no more for the inner loop, but there will be messages for all iterations of the outer loop.

Albert Retey
  • 23,585
  • 60
  • 104