AbortProtect[expr]
evaluates expr, saving any aborts until the evaluation is complete.
AbortProtect
AbortProtect[expr]
evaluates expr, saving any aborts until the evaluation is complete.
Details
- Aborts that are generated during an AbortProtect take effect as soon as the execution of the AbortProtect is over.
- CheckAbort can be used inside AbortProtect to catch and absorb any aborts that occur. »
- AbortProtect also protects against aborts generated by TimeConstrained and MemoryConstrained.
Examples
open all close allBasic Examples (2)
Protect part of a computation from early termination:
AbortProtect[Abort[];Print[a]];Print[b]Use AbortProtect to ensure an assignment is made:
AbortProtect[x = (3;Abort[];4)]Although the computation returned $Aborted, x was assigned the value of the computation:
xWithout the AbortProtect, the assignment would not have been made:
y = (3;Abort[];4)yScope (1)
Make sure that an iteration is finished in a time-constrained computation:
TimeConstrained[x = 1 / 10;Do[AbortProtect[imax = i;x = 4 x(1 - x)], {i, 1000000}], 1];{imax, N[x]}Without AbortProtect, typically imax is updated, but not x:
Table[TimeConstrained[x = 1 / 10;Do[imax = i;x = 4 x(1 - x), {i, 1000000}], .5];
{imax, N[x]}, {4}]Properties & Relations (5)
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["past CheckAbort"]]An enclosing AbortProtect is overridden by TimeConstrained:
AbsoluteTiming[AbortProtect[TimeConstrained[Pause[3];Echo["eval"], .1, Echo["constraint"]]]]While code inside the TimeConstrained is terminated, AbortProtect runs to completion:
AbsoluteTiming[AbortProtect[TimeConstrained[Pause[3];Echo["eval"], .1, Echo["constraint"]];Echo["past TimeConstrained"]]]A nested AbortProtect can block the action of TimeConstrained:
AbsoluteTiming[TimeConstrained[AbortProtect[Pause[3];Echo["eval"]], .1, Echo["constraint"]]]An enclosing AbortProtect is overridden by MemoryConstrained:
AbortProtect[MemoryConstrained[RandomReal[1, 10^5];Echo["eval"], 10^4, Echo["constraint"]]]While code inside the MemoryConstrained is terminated, AbortProtect runs to completion:
AbortProtect[MemoryConstrained[RandomReal[1, 10 ^ 5];Echo["eval"], 10^4, Echo["constraint"]];Echo["past MemoryConstrained"]]A nested AbortProtect can block the action of MemoryConstrained:
MemoryConstrained[AbortProtect[x = Range[10^4];Echo["eval"]], 10^2, Echo["constraint"]]Tech Notes
Related Guides
History
Introduced in 1991 (2.0)
Text
Wolfram Research (1991), AbortProtect, Wolfram Language function, https://reference.wolfram.com/language/ref/AbortProtect.html.
CMS
Wolfram Language. 1991. "AbortProtect." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/AbortProtect.html.
APA
Wolfram Language. (1991). AbortProtect. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/AbortProtect.html
BibTeX
@misc{reference.wolfram_2026_abortprotect, author="Wolfram Research", title="{AbortProtect}", year="1991", howpublished="\url{https://reference.wolfram.com/language/ref/AbortProtect.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_abortprotect, organization={Wolfram Research}, title={AbortProtect}, year={1991}, url={https://reference.wolfram.com/language/ref/AbortProtect.html}, note=[Accessed: 12-June-2026]}