WithCleanup[expr,cleanup]
evaluates expr, running cleanup before returning the result, even if an abort, throw, etc. was generated during the evaluation of expr.
WithCleanup[init,expr,cleanup]
evaluates init before evaluating expr, blocking aborts, throws, etc. in both init and cleanup.


WithCleanup
WithCleanup[expr,cleanup]
evaluates expr, running cleanup before returning the result, even if an abort, throw, etc. was generated during the evaluation of expr.
WithCleanup[init,expr,cleanup]
evaluates init before evaluating expr, blocking aborts, throws, etc. in both init and cleanup.
Details

- The following types of operations are blocked in various ways in init and cleanup in WithCleanup:
-
aborts Abort and interactive aborts constraints termination based on TimeConstrained, MemoryConstrained control transfers Throw, Confirm, Goto, Break, Continue, … - If an abort is initiated during the evaluation of init, it is not immediately acted upon, and expr is not evaluated, but cleanup is.
- If an abort is initiated during the evaluation of expr, that evaluation is aborted, but cleanup is still executed.
- If an abort is initiated during the evaluation of cleanup, the abort is postponed until the evaluation of cleanup is complete.
- Constraints generated externally through constructions such as TimeConstrained[... WithCleanup[...]... ] behave in WithCleanup like aborts.
- If a control transfer whose destination is outside WithCleanup[...] occurs within init or expr, the evaluation of init or expr is terminated, cleanup is evaluated, and then the transfer occurs.
- If a control transfer occurs during the evaluation of cleanup, it is not postponed.
Examples
open all close allBasic Examples (2)
Scope (2)
The cleanup is executed even though the evaluation is aborted:
The cleanup is done if an external constraint is reached during the evaluation of the main expression:
Constraints internal to WithCleanup are not treated specially:
Applications (1)
Properties & Relations (3)
An abort generated during initialization or cleanup will not prevent them from evaluating completely:
An abort during the evaluation of the main expression will cause its evaluation to end immediately:
An external control transfer in any argument will immediately end that argument's evaluation:
Note that the evaluation of the cleanup will happen if the transfer starts in an earlier argument:
In the last argument, the control transfer takes effect immediately:
The initialization and cleanup are effectively wrapped in AbortProtect:
See Also
AbortProtect CompoundExpression Block Once Close DeleteFile
Function Repository: BlockProtected
Related Guides
History
Text
Wolfram Research (2020), WithCleanup, Wolfram Language function, https://reference.wolfram.com/language/ref/WithCleanup.html.
CMS
Wolfram Language. 2020. "WithCleanup." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/WithCleanup.html.
APA
Wolfram Language. (2020). WithCleanup. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WithCleanup.html
BibTeX
@misc{reference.wolfram_2025_withcleanup, author="Wolfram Research", title="{WithCleanup}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/WithCleanup.html}", note=[Accessed: 14-August-2025]}
BibLaTeX
@online{reference.wolfram_2025_withcleanup, organization={Wolfram Research}, title={WithCleanup}, year={2020}, url={https://reference.wolfram.com/language/ref/WithCleanup.html}, note=[Accessed: 14-August-2025]}