Function Repository Resource:

AbortOnMessage

Source Notebook

Abort an evaluation as soon as a message is generated

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["AbortOnMessage"][eval]

aborts evaluation of eval if a message occurs.

ResourceFunction["AbortOnMessage"][eval,quiet]

aborts on message but disregards messages in quiet.

ResourceFunction["AbortOnMessage"][eval,quiet,listen]

forces an abort for any message in listen, even if it is quieted.

Details and Options

You can call ResourceFunction["AbortOnMessage"] anywhere within a computation. If it generates an abort, it has the same effect as an interactive interrupt in which you select the abort option.
You can use ResourceFunction["AbortOnMessage"] as an "emergency stop" in a computation as soon as the first message is printed.
Once ResourceFunction["AbortOnMessage"] has been called, Wolfram Language functions currently being evaluated return as quickly as possible.
In an interactive session, the final result from an aborted computation is $Aborted.
You can use CheckAbort to "catch" returns from an abort.

Examples

Basic Examples (3) 

Abort an evaluation as soon as a message occurs:

In[1]:=
ResourceFunction["AbortOnMessage"][Table[First[i], {i, 5}]]
Out[1]=

Ignore some messages:

In[2]:=
ResourceFunction["AbortOnMessage"][
 Table[First[i], {i, 5}], {First::normal}]
Out[2]=

Trigger an abort even if a message is suppressed with Quiet:

In[3]:=
ResourceFunction["AbortOnMessage"][
 Quiet[1/0, {Power::infy}], None, {Power::infy}]
Out[3]=

Properties and Relations (2) 

AbortOnMessage will stop an evaluation rather than just suppress additional messages:

In[4]:=
ResourceFunction["AbortOnMessage"][
 Table[Print[Last[Range[-n]]], {n, -2, 2}]]
Out[4]=

Use CheckAbort to “catch” aborts and replace with some other expression rather than aborting an entire evaluation:

In[5]:=
Table[CheckAbort[
  ResourceFunction["AbortOnMessage"][1/n], $Failed], {n, -3, 3}]
Out[5]=

Possible Issues (1) 

Any messages given in the third argument will take precedence over the second argument:

In[6]:=
ResourceFunction["AbortOnMessage"][
 Table[First[1], 5], {First::normal}, {}]
Out[6]=
In[7]:=
ResourceFunction["AbortOnMessage"][
 Table[First[1], 5], {First::normal}, {First::normal}]
Out[7]=

Version History

  • 2.0.0 – 12 August 2019
  • 1.0.0 – 15 August 2018

Related Resources

License Information