CheckAbort[expr,failexpr]
evaluates expr, returning failexpr if an abort occurs.
CheckAbort
CheckAbort[expr,failexpr]
evaluates expr, returning failexpr if an abort occurs.
Details and Options
- By default, CheckAbort absorbs any user aborts (including calls to Abort[]) it handles, and does not propagate them further.
- Whether CheckAbort absorbs the aborts it handles can be changed with option PropagateAborts, which takes the following settings:
-
Automatic propagate aborts other than user-initiated aborts (including calls to Abort[]) True propagate all aborts False propagate no aborts - CheckAbort works inside AbortProtect. »
- CheckAbort has attribute HoldAll.
Examples
open all close allBasic Examples (1)
Scope (3)
Use CheckAbort inside AbortProtect to detect aborts:
AbortProtect[Abort[];CheckAbort[Echo["no abort"], Echo["yes abort"]];Echo["end of AbortProtect"]]The return value may depend on the function that generated the abort:
TimeConstrained[AbortProtect[Pause[1];CheckAbort[Echo["no abort"], Echo["yes abort"]];Echo["end of AbortProtect"]], .5, "Timed out"]Use PropagateAborts to control whether aborts continue or terminate after CheckAbort:
AbortProtect[Abort[];CheckAbort[Echo["A1"], Echo["A2"], PropagateAborts -> True];
CheckAbort[Echo["B1"], Echo["B2"], PropagateAborts -> False];CheckAbort[Echo["C1"], Echo["c2"], PropagateAborts -> False];Echo["finished AbortProtect"]]Use CheckAbort inside TimeConstrained to select which parts are skipped if the limit is reached:
TimeConstrained[CheckAbort[Pause[1];Echo["first"], Echo["second"], PropagateAborts -> False];Echo["finished CheckAbort"], .1]This can also be done with MemoryConstrained:
MemoryConstrained[CheckAbort[Range[10 ^ 6];Echo["first"], Echo["second"], PropagateAborts -> False];Echo["finished CheckAbort"], 10 ^ 4]Options (2)
PropagateAborts (2)
The default setting, Automatic, does not propagate user-initiated aborts outside of CheckAbort:
CheckAbort[Abort[], Echo["check"], PropagateAborts -> Automatic];Echo["past CheckAbort"]Use the setting PropagateAbortsTrue to force the abort to persist past the CheckAbort:
CheckAbort[Abort[], Echo["check"], PropagateAborts -> True];Echo["past CheckAbort"]Aborts created by TimeConstrained are by default propagated:
TimeConstrained[
CheckAbort[Pause[1], Echo["check"], PropagateAborts -> Automatic];Echo["past CheckAbort"], .1]Use the setting PropagateAbortsFalse to prevent the abort from persisting:
TimeConstrained[
CheckAbort[Pause[1], Echo["check"], PropagateAborts -> False];Echo["past CheckAbort"], .1]Aborts created by MemoryConstrained are similar:
MemoryConstrained[
CheckAbort[Range[10 ^ 6], Echo["check"], PropagateAborts -> Automatic];Echo["past CheckAbort"], 10 ^ 4]Properties & Relations (5)
The evaluation of the second argument cannot be interrupted once begun:
AbsoluteTiming[TimeConstrained[CheckAbort[Abort[], Pause[3];Echo["check"]], 0.1, Echo["constraint"]]]The argument is effectively wrapped in AbortProtect:
AbsoluteTiming[TimeConstrained[CheckAbort[Abort[], AbortProtect[Pause[3];Echo["check"]]], 0.1, Echo["constraint"]]]CheckAbort enables and detects aborts even if enclosed within AbortProtect:
AbortProtect[
CheckAbort[Echo["before"];Abort[];Echo["after"],
Echo["check"]]]While the computation inside the CheckAbort is terminated, AbortProtect runs to completion:
AbortProtect[
CheckAbort[Echo["before"];Abort[];Echo["after"],
Echo["check"]];Echo["passed CheckAbort"]]A nested AbortProtect will delay an abort but not prevent it from being detected by CheckAbort:
CheckAbort[
AbortProtect[Echo["before"];Abort[];Echo["after"]],
Echo["check"]]If an abort is already active when CheckAbort begins, the first argument will not be evaluated:
TimeConstrained[AbortProtect[Pause[1];CheckAbort[Echo["first"], Echo["second"]];Echo["finished AbortProtect"]], .1]CheckAbort can be used to perform cleanup if a computation is interrupted:
f[x_] := Module[{y}, CheckAbort[$myFlag = True;If[EvenQ[x], y = 2x, Abort[]];$myFlag = False;y,
$myFlag = False;$Aborted
]
]$myFlag is set to False even for inputs that produce an abort:
f[3]$myFlagWithCleanup accomplishes this same goal with significantly less code:
g[x_] := WithCleanup[$myFlag = True, If[EvenQ[x], 2x, Abort[]],
$myFlag = False]g[3]$myFlagTech Notes
Related Guides
History
Introduced in 1991 (2.0) | Updated in 2022 (13.2)
Text
Wolfram Research (1991), CheckAbort, Wolfram Language function, https://reference.wolfram.com/language/ref/CheckAbort.html (updated 2022).
CMS
Wolfram Language. 1991. "CheckAbort." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/CheckAbort.html.
APA
Wolfram Language. (1991). CheckAbort. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CheckAbort.html
BibTeX
@misc{reference.wolfram_2026_checkabort, author="Wolfram Research", title="{CheckAbort}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/CheckAbort.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_checkabort, organization={Wolfram Research}, title={CheckAbort}, year={2022}, url={https://reference.wolfram.com/language/ref/CheckAbort.html}, note=[Accessed: 12-June-2026]}