MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: immediate abort at error messages

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55906] Re: [mg55764] immediate abort at error messages
  • From: Omega Consulting <info at omegaconsultinggroup.com>
  • Date: Sat, 9 Apr 2005 03:56:17 -0400 (EDT)
  • References: <200504051010.GAA01066@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

This is quite a creative trick.

Unfortunately, it doesn't look like you can use it and get the message. 
$Messages is a list of OutputStreams that any messages are written to. 
If the OutputStream is stdout, then you get the normal message. If it 
is an OutputStream to a file, then it is written to that file. So I 
thought that the following might work, but it doesn't:

$Messages := {OutputStream["stdout",1], Abort[]}

Why not? Because $Messages is evaluated *before* the messages are 
actually sent to the streams.

Still there is another trick we can use, called a trap. A message is 
created whenever the Message function is called. We can redefine this 
function by unprotecting it.

In[1]:= Unprotect[Message];

Now we create a global variable to indicate what we want to do with a 
Message. When the variable is True, we want to deviate from the default 
behavior. When False, we want the default behavior (i.e. a message).

In[2]:= $AbortMessage=True;

Now we add a definition for when the variable is True. It calls Message 
with the variable set to False (creating a message) and then Aborts.

In[3]:=
Message[args___] :=
  (Block[{$AbortMessage=False},
     Message[args]
     ]; Abort[]
    )/;$AbortMessage

To be safe we re-protect Message.

In[4]:= Protect[Message];

Now we try it.

In[5]:= (Sin[2,2];Print[2])
Sin::"argx": "Sin called with 2 arguments; 1 argument is expected. 
More?"
Out[5]= $Aborted

----------------------------------------------
Omega Consulting
The final answer to your Mathematica needs.
http://omegaconsultinggroup.com
On Apr 5, 2005, at 5:10 AM, Marcus Stollsteimer wrote:

> Hi group,
>
> I recently learned that there is a way to immediately abort
> when an error occurs in the middle of a cell, so that Mathematica
> does not try to evaluate the subsequent expressions with
> possibly bad input:
>
> with
>
>   $Messages:=Abort[]
>
> (see message <2430df44.0411240157.4d06479a at posting.google.com>)
>
> but now, when an error occurs, Mathematica aborts without any 
> message...
> it would be useful to still see the error message,
> is there a way to do this (display message, then abort)?
>
> Best regards,
> Marcus
>
> -- 
> Money is better than poverty, if only for financial reasons.
>    -- W. Allen
>


  • Prev by Date: Re: Replacement gyrations
  • Next by Date: Re: Replacement gyrations
  • Previous by thread: Re: immediate abort at error messages
  • Next by thread: Re: Re: immediate abort at error messages