MathGroup Archive 2012

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

Search the Archive

Re: Abort computation on any message

  • To: mathgroup at smc.vnet.net
  • Subject: [mg124908] Re: Abort computation on any message
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Sun, 12 Feb 2012 04:58:34 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jh2ti3$tc$1@smc.vnet.net>

On 10/02/2012 11:03, Szabolcs wrote:
> Messages are never fatal in Mathematica, regardless of whether they indicate errors, warnings or they're just informative.  Evaluations continue even after messages are generated.
>
> Question: How can I get Mathematica to abort immediately as soon as a message is generated?
>
> I need the following behaviour:
>
>    1. if a message would be printed, always abort.
>    2. if no message would be printed, don't abort.
>    3. do print the first message so I have a hint about what went wrong
>    4. the mechanism should work with parallel computations and should abort the full evaluation (i.e. main kernel and all parallel kernels)
>
> Difficulties are caused by the many ways to control message printing:  On/Off, Quiet, etc.  My code does use Quiet, but I am quieting only certain messages.
>

First, I'd like to say that you have put your finger on an aspect of 
Mathematica that could really do with some work by WRI. The message 
system seems quite archaic to me, with its fixed number of message 
repeats per evaluation, and its lack of a way to stop immediately after 
a message - or to display the message in another way!

I encountered this issue when I built my DebugTrace debugger (free from 
my website), but in this context the problem was easier to solve, 
because the debugger injects extra code into the program. However, it 
would seem that the general problem is (just) solvable using this 
construction:

Unprotect[Message];
pass = False;
Message[mes_MessageName, args___] :=
   Block[{pass = True}, Message[mes, args];
     Abort[]] /; (Head[mes] === MessageName && ! pass);
Protect[Message];

I tested this with this function, using Off on the message to ensure it 
did not abort in this case. I used Mathematica 8.0 on Windows XP 64-bit.

g[] := Module[{},
    k = 3;
    While[k >= -3,
     Print[1.0/k];
     k--;
     ];
    ];

One point to note, is that fn::message evaluates to something with head 
$Off, if the message has been suppressed with Off. Message has HoldFirst 
attribute, so you can't just write mes_MessageName to check this. I 
haven't tested all cases in which groups of messages are suppressed. 
Also, I am also not sure all messages actually go through the Message 
function - so this may only be a first step!

I find the above construct very useful for tweaking the operation of 
built in functions! Of course, this only really makes sense for 
debugging aids and the like, because things may change with future 
versions of Mathematica.

Please contact me if this is not a complete solution.

David Bailey
http://www.dbaileyconsultancy.co.uk



  • Prev by Date: Re: Some assistance from seasoned users.
  • Next by Date: Re: Need help with prime Test
  • Previous by thread: Re: Abort computation on any message
  • Next by thread: Re: Abort computation on any message