evaluates expr, running cleanup before returning the result, even if an abort, throw, etc. was generated during the evaluation of expr.
evaluates init before evaluating expr, blocking aborts, throws, etc. in both init and cleanup.
- 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.
Examplesopen allclose all
Basic Examples (2)
Constraints internal to WithCleanup are not treated specially:
Properties & Relations (3)
The initialization and cleanup are effectively wrapped in AbortProtect:
Wolfram Research (2020), WithCleanup, Wolfram Language function, https://reference.wolfram.com/language/ref/WithCleanup.html.
Wolfram Language. 2020. "WithCleanup." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/WithCleanup.html.
Wolfram Language. (2020). WithCleanup. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WithCleanup.html