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:
  • abortsAbort and interactive aborts
    constraintstermination based on TimeConstrained, MemoryConstrained
    control transfersThrow, 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 allclose all

Basic Examples  (2)

Clean up the assignment to x before returning the result:

The cleanup is done even if there is an abort in the main expression being evaluated:

The same happens with other types of exceptions:

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)

Ensure that work files are properly closed even if a computation is interrupted:

The data was safely written to disk:

Similarly, ensure that temporary files are deleted no matter how a computation ends:

The file was successfully deleted:

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:

Possible Issues  (1)

Computations in the initialization and cleanup cannot be interactively aborted or time-constrained:

Move all nonessential code into the main expression:

Wolfram Research (2020), WithCleanup, Wolfram Language function, https://reference.wolfram.com/language/ref/WithCleanup.html.

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_2024_withcleanup, author="Wolfram Research", title="{WithCleanup}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/WithCleanup.html}", note=[Accessed: 21-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_withcleanup, organization={Wolfram Research}, title={WithCleanup}, year={2020}, url={https://reference.wolfram.com/language/ref/WithCleanup.html}, note=[Accessed: 21-November-2024 ]}