Message Boards Message Boards

1
|
4158 Views
|
5 Replies
|
7 Total Likes
View groups...
Share
Share this post:

How to capture an error message to print it out later?

Posted 11 years ago
I want to capture an error message so I can print it out later. For exmaple, I can capture the the error message for division by zero in the following by setting myerror equal to the $MessageList as long as it is on the same line, apparently. And by repeating myerror on the next line, the output proves that I have captures the "Power::infy" error message. But the specific error statement "Infinite expression 1/0 encountered. >>" I don't know how to capture.

Executing this code
c = 1/0; myerror= $MessageList
myerror
produces this output:
Power::infy: Infinite expression 1/0 encountered. >>

{Power::infy}

{Power::infy}
So, how do I capture the specific error message "Infinite expression 1/0 encountered. >> " ?
Thanks!
POSTED BY: Danny Mills
5 Replies
You can use $MessagePrePrint to format the message arguments:
$MessagePrePrint = Function[{e}, InputForm[e]]
will make this:
1/0;
give this message:
Power::infy: Infinite expression HoldForm[0^(-1)] encountered.
The HoldForm is required in messages, to avoid an infinite recursion (and InputForm always displays HoldForm as is).
If you don't like the HoldForm, you could convert the HoldForm to a string and remove that:
$MessagePrePrint = Function[{e}, StringTake[ToString[e, InputForm], {10, -2}]]
which gives:
Power::infy: Infinite expression 0^(-1) encountered.
POSTED BY: Arnoud Buzing
Posted 11 years ago
How do I get
OpenWrite["message.out"];
to use the FormatType "InputForm" one-dimensional form, so I don't get the two-dimensional fractions like
                                 1
Power::infy: Infinite expression - encountered.
                                 0
when using
FilePrint["message.out"]
POSTED BY: Danny Mills
Posted 11 years ago
Thanks to the Moderation Team for the tip.  Thanks to Arnoud Buzing for the info.
POSTED BY: Danny Mills
One way would be to add your own output stream to $Messages, which is a list that holds the output streams that messages are written to. Normally $Messages is a list of one output stream, namely the standard output stream (stdout). So this opens an output stream and appends it to $Messages:
$MyMessageLogger = OpenWrite["message.out"];
AppendTo[$Messages, $MyMessageLogger];
After that you can evaluate things that generate messages:
1/0;
And you can look at the content using FilePrint (or Import or OpenRead):
FilePrint["message.out"]
which gives:
                                 1
Power::infy: Infinite expression - encountered.
                                 0
If you don't want to see any messages on your screen, just set $Messages to your output stream (instead of using AppendTo):
$Messages={$MyMessageLogger];
POSTED BY: Arnoud Buzing
Your comment on another post is a separate question and it was re-posted as a new discussion. Please in future do not ask new questions on existing discussions but submit them as new. Also note code-box formatting and please follow it in future.
POSTED BY: Moderation Team
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract