Skip to main content

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. >>"
{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?



Answer



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


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

Mathematica graphics


Internal`RemoveHandler["Message", collectMessages]


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


Comments