MathGroup Archive 2011

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

Search the Archive

Re: Aborting execution on first message

  • To: mathgroup at
  • Subject: [mg115274] Re: Aborting execution on first message
  • From: Leonid Shifrin <lshifr at>
  • Date: Thu, 6 Jan 2011 02:04:00 -0500 (EST)

Hi Yaroslav,

This is possible. I suggest to use the Block trick, which is generally a
very powerful and underrated mechanism (but also  dangerous, since you can
corrupt the kernel in subtle ways if you use it without care). Here is a
minimal example:

f::noodd = "The input is not an odd number";
f::nonum = "The input is not of the right type";
f[x_?OddQ] := x^2;
f[x_?NumericQ] := "" /; Message[f::noodd];
f[_] := "" /; Message[f::nonum];
f[___] := $Failed;

In[37]:= f[2]

During evaluation of In[37]:= f::noodd: The input is not an odd number

During evaluation of In[37]:= f::nonum: The input is not of the right type

Out[37]= $Failed

Here is the simplest (but really not very elegant) version:

In[38]:= Block[{Message},
 Message[args__] := Abort[];

Out[38]= $Aborted

Here is a more elaborate version which allows you to get some information
about the execution near the point at which we stop:

SetAttributes[execAndAbortOnMessage, HoldAll];
execAndAbortOnMessage[code_, actionCode_: Automatic] :=
   If[Unevaluated[actionCode] === Automatic,
    Message[args__] := (Print["Aborted at ", Stack[_][[-5]]];
    (* else *)
    Message[args__] := (Print["Stopped at ", Stack[_][[-5]]];

For example:

In[39]:= execAndAbortOnMessage[f[2]]

During evaluation of In[39]:= Aborted at f[2]

Out[39]= $Aborted


In[40]:= execAndAbortOnMessage[f[2], Throw[$Failed]]

During evaluation of In[40]:= Stopped at f[2]

During evaluation of In[40]:= Throw::nocatch: Uncaught Throw[$Failed]
returned to top level. >>

Out[40]= Hold[Throw[$Failed]]

As you see, the above function allows you to execute arbitrary code when you
stop the evaluation. Note that using Abort[] is really the last resort. I
understand that you probably want it for debugging, but even so, often
softer ways like exception throwing as above will offer you more
possibilities. You can use exceptions with tags, for example, to execute
different code depending on the error.

I did not test the function extensively, so it may not always behave as
intended, but at least this is some starting point. You can play with Stack[
] further if you need to extract more information about the evaluation near
the point where we abort it.

Hope this helps.


On Tue, Jan 4, 2011 at 3:52 PM, Yaroslav Bulatov <yaroslavvb at>wrote:

> Is there a way to Abort execution on the first message generated?
> Essentially, I'm looking to mimic behavior of "Break at Message" of
> the Debugger in normal mode

  • Prev by Date: Re: Session Privacy in Mathematica 8 w/ Wolfram|Alpha free-form input?
  • Next by Date: Re: Using Extract where some indices are out of bounds (efficiently)
  • Previous by thread: Aborting execution on first message
  • Next by thread: Re: Aborting execution on first message