Re: To: Omega Consulting, Problem with $AbortMessage trap (The Mathematica Journal).

1 view
Skip to first unread message

Carl Woll

unread,
Nov 4, 2006, 4:10:41 AM11/4/06
to
Philipp wrote:

>I've been working on an extension to your solution of immediate
>interception of Message[] to make it sensitive only to specified
>messages (analogous to the Check[]).
>
>But this aside, I have encountered a curious behaviour of your original
>solution, namely
>
>In[]:= Unprotect[Message];
> Message[args___] :=
> (Block[{$AbortMessageQ = False}, Message[args]]; Abort[]) /;
> $AbortMessageQ
> Protect[Message];
>
>
>In[]:= $AbortMessageQ = False;
>
>
>In[]:= (Log[1, 1]; Prime[-120]; StringLength[ToString[N[Pi, 10^5]]])
>
> General::"dbyz": "Division by zero."
>
> \[Infinity]::"indet": "Indeterminate expression
> 0 ComplexInfinity encountered."
>
> Prime::"intpp": "Positive integer argument expected
> in Prime[-120]."
>
>Out[]:= 100001
>
>Now setting he control variable to True
>
>In[]:= $AbortMessageQ = True;
>
>gives the expected result, i.e.,
>
> General::"dbyz": "Division by zero."
>
>Out[]:= $Aborted
>
>However, using Off[] to turn off the first message,
>
>In[]:= Off[General::"dbyz"]
>
>In[]:= (Log[1, 1]; Prime[-120]; StringLength[ToString[N[Pi, 10^5]]])
>Out[]:= $Aborted
>
>fails to print the message.
>
>Could you please explain such a curious behaviour, and maybe provide a
>remedy?
>
>Cheers,
>Philip
>
>
You need to add another condition to your Message redefinition:

Unprotect[Message];
Message[args___] := Block[{$AbortMessageQ = False},
Message[args]; Abort[]
] /; $AbortMessageQ && !MatchQ[{args}, {$Off[], ___}]
Protect[Message];

This way we don't abort on messages that have been turned off.

Carl Woll
Wolfram Research

Reply all
Reply to author
Forward
0 new messages