Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Re: Stop on message?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126786] Re: Stop on message?
  • From: Christoph Lhotka <christoph.lhotka at fundp.ac.be>
  • Date: Thu, 7 Jun 2012 05:20:43 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201205260911.FAA07083@smc.vnet.net> <201206060852.EAA17914@smc.vnet.net>

Dear all,

I would like to share with you a simple way of debugging your
code in the development phase of your project using standard
Mathematica code instead of modifying internal functions of
the system (which may also imply unpredictable behaviour):

     $Pre = If[Length[$MessageList] > 0, Interrupt[], #] &;

The code will open the "Interrupt Evaluation" dialog whenever
a message is generated during evaluation. You decide to abort,
enter a sub-session (to debug) or to continue to the next
step of the evaluation chain.

(If you want to prevent Mathematica to send the messages to
the standard output channel you redirect it to a file or another
notebook/channel using $Messages. See ?$Messages)

************

A simple example: let me assume that I wrote /0 instead of ==0 in the
following expression:

     sol = Solve[2 x - 3 y/0, y];
     Plot[y /. sol, {x, 0, 1}]

Without setting $Pre (Clear[$Pre] before) you will get 3 error messages
and an empty plot.

Using the assignment for $Pre I can enter a sub-session, correct the
variable sol just by typing

     sol=Solve[2 x - 3 y==0, y]
     {{y->(2 x)/3}}

return to the main loop using

     Return[]

and get the right plot.

Copy & paste the right expression to the original input, clear
$Pre - no message produced - problem solved.

Personally, I would avoid to modify internal stuff of Mathematica.

Best,

Christoph


On 06/06/2012 10:52 AM, Ralph Dratman wrote:
> Re: the recent "stop on message" discussion. The earlier-posted
> solution from StackExchange suggests adding the following to the top
> of a notebook:
> - - - - - - - - - -
> messageHandler = If[Last[#], Abort[]]&
> Internal`AddHandler["Message", messageHandler]
> (* The above will abort the computation whenever a message would be printed=
> . *)
> (* It can be turned off using*)
> (* Internal`RemoveHandler["Message", messageHandler] *)
> - - - - - - - - - -
> This helps, and seems to prevent a complete freeze, but it does not
> always terminate the evaluation immediately. The Abort call seems to
> wait for something to finish before stopping everything.
>
> > From prior experience, I knew that Interrupt works more quickly than
> Abort. I realize this seems a little bit illogical, but nevertheless,
> that is my observation: Interrupt gets you out of trouble instantly,
> while Abort often doesn't.
>
> I substituted Interrupt in the handler, with much better results, as follow=
> s:
> - - - - - - - - - -
> (* ----- proposed new version ----- *)
> messageHandler = If[Last[#], Interrupt[]]&
> Internal`AddHandler["Message", messageHandler]
> (* The above will immediately (!) put up an Abort dialog offering to *)
> (* end the evaluation whenever an enabled message is issued. *)
> (* It can be turned off using *)
> (* Internal`RemoveHandler["Message", messageHandler] *)
> - - - - - - - - - -
> This tiny enhancement to Mathematica has proved amazingly helpful, and
> I hope everyone will try it. It is a relief not to be always
> anticipating the next messy crash.
>
> Let me know if you have any additions or corrections.
>
> Ralph Dratman
>
>
> On Tue, May 29, 2012 at 5:47 AM, Szabolcs Horv=E1t<szhorvat at gmail.com>  wro=
> te:
>> Please see here for a method that will reliably abort immediately when a
>> message is generated.
>> http://mathematica.stackexchange.com/questions/1512/how-to-abort-on-any-m=
> essage-generated
>




  • Prev by Date: Re: Memory Blowup Issues
  • Next by Date: Re: Can Mathematica do regression, or similar?
  • Previous by thread: Re: Stop on message?
  • Next by thread: Using CountryData to Get US Population by State