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 allclose allScope (3)
Use CheckAbort inside AbortProtect to detect aborts:
The return value may depend on the function that generated the abort:
Use PropagateAborts to control whether aborts continue or terminate after CheckAbort:
Use CheckAbort inside TimeConstrained to select which parts are skipped if the limit is reached:
This can also be done with MemoryConstrained:
Options (2)
PropagateAborts (2)
The default setting, Automatic, does not propagate user-initiated aborts outside of CheckAbort:
Use the setting PropagateAbortsTrue to force the abort to persist past the CheckAbort:
Aborts created by TimeConstrained are by default propagated:
Use the setting PropagateAbortsFalse to prevent the abort from persisting:
Aborts created by MemoryConstrained are similar:
Properties & Relations (5)
The evaluation of the second argument cannot be interrupted once begun:
The argument is effectively wrapped in AbortProtect:
CheckAbort enables and detects aborts even if enclosed within AbortProtect:
While the computation inside the CheckAbort is terminated, AbortProtect runs to completion:
A nested AbortProtect will delay an abort but not prevent it from being detected by CheckAbort:
If an abort is already active when CheckAbort begins, the first argument will not be evaluated:
CheckAbort can be used to perform cleanup if a computation is interrupted:
$myFlag is set to False even for inputs that produce an abort:
WithCleanup accomplishes this same goal with significantly less code:
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