Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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. >>"
{Power::infy,mrtError::function}

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
add comment

4 Answers

up vote 8 down vote accepted

Here's another, reliable way:

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

Then do

clearMessages[]
1/0; 0/0;
messages

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
1  
@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
add comment

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

$MessagePrePrint = Sow;

Reap[
 Module[{}, 1/0; 0^0]; $MessageList
]
{{Power::infy,Power::indet},{{1/0,0^0}}}

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... –  rm -rf 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
add comment

You could always capture the information directly,

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

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

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
add comment

Your Answer

 
discard

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.