Interrupts and Aborts

"Interrupting Calculations" describes how you can interrupt a Wolfram Language computation by pressing appropriate keys on your keyboard.

In some cases, you may want to simulate such interrupts from within a Wolfram Language program. In general, executing (RefLink[Interrupt, paclet:ref/Interrupt])[] has the same effect as pressing interrupt keys. On a typical system, a menu of options is displayed, as discussed in "Interrupting Calculations".

Interrupts and aborts.

The function (RefLink[Abort, paclet:ref/Abort])[] has the same effect as interrupting a computation, and selecting the abort option in the interrupt menu.

You can use (RefLink[Abort, paclet:ref/Abort])[] to implement an "emergency stop" in a program. In almost all cases, however, you should try to use functions like (RefLink[Return, paclet:ref/Return]) and (RefLink[Throw, paclet:ref/Throw]), which lead to more controlled behavior.

(RefLink[Abort, paclet:ref/Abort]) terminates the computation, so only the first (RefLink[Print, paclet:ref/Print]) is executed:

Print[a];Abort[];Print[b]

If you abort at any point during the evaluation of a Wolfram Language expression, the Wolfram Language normally abandons the evaluation of the whole expression, and returns the value (RefLink[$Aborted, paclet:ref/$Aborted]).

You can, however, "catch" aborts using the function (RefLink[CheckAbort, paclet:ref/CheckAbort]). If an abort occurs during the evaluation of expr in (RefLink[CheckAbort, paclet:ref/CheckAbort])[expr,failexpr], then (RefLink[CheckAbort, paclet:ref/CheckAbort]) returns failexpr, but the abort propagates no further. Functions like (RefLink[Dialog, paclet:ref/Dialog]) use (RefLink[CheckAbort, paclet:ref/CheckAbort]) in this way to contain the effect of aborts.

(RefLink[CheckAbort, paclet:ref/CheckAbort]) catches the abort, prints c and returns the value aborted:

CheckAbort[Print[a];Abort[];Print[b],Print[c];aborted]

The effect of the (RefLink[Abort, paclet:ref/Abort]) is contained by (RefLink[CheckAbort, paclet:ref/CheckAbort]), so b is printed:

CheckAbort[Print[a];Abort[],Print[c];aborted];Print[b]

When you construct sophisticated programs in the Wolfram Language, you may sometimes want to guarantee that a particular section of code in a program cannot be aborted, either interactively or by calling (RefLink[Abort, paclet:ref/Abort]). The function (RefLink[AbortProtect, paclet:ref/AbortProtect]) allows you to evaluate an expression, saving up any aborts until after the evaluation of the expression is complete.

The (RefLink[Abort, paclet:ref/Abort]) is saved up until (RefLink[AbortProtect, paclet:ref/AbortProtect]) is finished:

AbortProtect[Abort[];Print[a]];Print[b]

The (RefLink[CheckAbort, paclet:ref/CheckAbort]) sees the abort, but does not propagate it further:

AbortProtect[Abort[];CheckAbort[Print[a],x]];Print[b]

Even inside (RefLink[AbortProtect, paclet:ref/AbortProtect]), (RefLink[CheckAbort, paclet:ref/CheckAbort]) will see any aborts that occur, and will return the appropriate failexpr. Unless this failexpr itself contains (RefLink[Abort, paclet:ref/Abort])[], the aborts will be "absorbed" by the (RefLink[CheckAbort, paclet:ref/CheckAbort]).