Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 17 down vote accepted

Here's another, reliable way:

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?

share|improve this answer
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 Feb 27 '13 at 15:36
@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 Feb 27 '13 at 15:44
@rcollyer I keep telling myself that but it pervades other facets of life I'm afraid. – Mr.Wizard Feb 27 '13 at 15:53
@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 Feb 27 '13 at 15:58
@rcollyer I changed my avatar to something more appropriate. (The thing is I'm not old, I just feel like it.) – Mr.Wizard Feb 27 '13 at 16:03

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

share|improve this answer
Or, just capture the info from Message directly. – rcollyer Feb 27 '13 at 15:31
@rcollyer okay, I agree that's better here. – Mr.Wizard Feb 27 '13 at 15:35
Given your new avatar, I'm compelled to bring back the Hypnotoad... – R. M. Feb 27 '13 at 16:12
@rm-rf -- do as you wish :-) – Mr.Wizard Feb 27 '13 at 16:13
@rm-rf that is a very odd toad. – Mr.Wizard Feb 27 '13 at 16:21

You could always capture the information directly,

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

(* {{Power::infy, HoldForm[1/0]}, {Power::indet, HoldForm[0^0]}} *)
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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