How to catch complete error message information, including the message text as it would be printed?
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 Quiet
ed 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
Internal`RemoveHandler["Message", collectMessages]
Reference and details: How to abort on any message generated?
In version 10 or later, we can use EvaluationData
.
EvaluationData[1/0; 0^0]
Behind the scenes, this uses handlers, like in my first answer, meaning that only those messages will be recorded which would get printed.
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