How to catch complete error message information, including the message text as it would be printed?



If I evaluate this expression:

Module[{}, 1/0;0^0]; msg = $MessageList

I get:

"Power::infy: Infinite expression 1/0 encountered. >>"
"Power::indet: Indeterminate expression 0^0 encountered. >>"

How can I collect the complete error messages in msg, instead of just the first part? Something for msg like:

{"Power::infy: Infinite expression 1/0 encountered. >>"
,"Power::indet: Indeterminate expression 0^0 encountered. >>"}

Some clue?


Posted 2013-02-27T15:02:50.957

Reputation: 23 859



Update See here for a documented way to do the very same thing in v10.0 or later.

This method will only catch those messages which would actually get printed, not those which are Quieted or turned Off.

We can use handlers:

messages = {}
clearMessages[] := messages = {}
collectMessages[m_] := AppendTo[messages, m]
Internal`AddHandler["Message", collectMessages]

Then do

1/0; 0/0;

Mathematica graphics

Internal`RemoveHandler["Message", collectMessages]

Reference and details: How to abort on any message generated?


Posted 2013-02-27T15:02:50.957

Reputation: 213 047

I really do worry about my failing brain. I was about to ask where you learned this but I've already voted on your answer in the referenced thread. Anyway a semi-senile +1. – Mr.Wizard – 2013-02-27T15:36:56.813

@Mr.Wizard you've voted ~4500 times, answered nearly 900 questions, and asked over 50 yourself. Sure, senility is the reason you can't remember what you've done before on this site. Absolutely. :) – rcollyer – 2013-02-27T15:44:13.213

@rcollyer I keep telling myself that but it pervades other facets of life I'm afraid. – Mr.Wizard – 2013-02-27T15:53:50.073

1@Mr.Wizard I think I'm younger than you, but I already suffer from CRS (can't remember "stuff"), and have for most of my life. So, I wouldn't worry about it. Besides, you'll forget about it in a couple of minutes anyway. :) – rcollyer – 2013-02-27T15:58:21.953

1@rcollyer I changed my avatar to something more appropriate. (The thing is I'm not old, I just feel like it.) – Mr.Wizard – 2013-02-27T16:03:58.970


In version 10 or later, we can use EvaluationData.

EvaluationData[1/0; 0^0]

Mathematica graphics

Behind the scenes, this uses handlers, like in my first answer, meaning that only those messages will be recorded which would get printed.


Posted 2013-02-27T15:02:50.957

Reputation: 213 047

Holy cow this is something I had no idea even existed. – Pillsy – 2018-10-31T14:14:44.553

1@Pillsy It's new in v10.0, and it has not been advertised much. Those of us who started much earlier and were used to different ways of doing things would easily miss it. – Szabolcs – 2018-10-31T17:44:36.323


Simply you could use $MessagePrePrint to get the "fillers" and $MessageList as you did to get the message name they belong to:

$MessagePrePrint = Sow;

 Module[{}, 1/0; 0^0]; $MessageList

For complete control you could go low-level and intercept MessagePacket as I did for:
Prepend Information to Warning Messages


Posted 2013-02-27T15:02:50.957

Reputation: 259 163

Or, just capture the info from Message directly.

– rcollyer – 2013-02-27T15:31:56.547

@rcollyer okay, I agree that's better here. – Mr.Wizard – 2013-02-27T15:35:11.910

Given your new avatar, I'm compelled to bring back the Hypnotoad... – rm -rf – 2013-02-27T16:12:38.377

@rm-rf -- do as you wish :-) – Mr.Wizard – 2013-02-27T16:13:10.017

1@rm-rf that is a very odd toad. – Mr.Wizard – 2013-02-27T16:21:26.957


The following method will capture all messages, regardless of whether they are Quieted or turned Off. Messages that are turned off will be wrapped in $Off.

You could always capture the information directly,

myMessageList = {};
Internal`InheritedBlock[{Message, $InMsg = False},
 Message[msg_, vars___] /; ! $InMsg :=
  Block[{$InMsg = True},
   AppendTo[myMessageList, {HoldForm[msg], vars}];
   Message[msg, vars]
 (* code to run *)
 Module[{}, 1/0; 0^0]

(* {{Power::infy, HoldForm[1/0]}, {Power::indet, HoldForm[0^0]}} *)


Posted 2013-02-27T15:02:50.957

Reputation: 32 561


You can use Messages[foo] to get the text of any message. With that, we can proceed as follows to extract the text of the messages that were last generated:

Module[{}, 1/0;0^0]; msg = $MessageList; (* last errors *)

With[{messages = ReleaseHold@
    DeleteDuplicates[# /. HoldPattern@MessageName[s_, _] :> Messages@s] &},

    # /. messages@#] &@msg
(* {{Infinite expression `1` encountered.,Indeterminate expression `1` encountered.}} *)

Note that you will not be able to retrieve whatever was inserted in the placeholder `1` with this.

rm -rf

Posted 2013-02-27T15:02:50.957

Reputation: 85 395