WithCleanup[expr,cleanup]
expr を評価し,expr の評価中に,放棄,スロー等が生成された場合でも,結果を返す前に cleanup を実行する.
WithCleanup[init,expr,cleanup]
expr の評価の前に init を評価し,init と cleanup の両方の放棄やスロー等をブロックする.
WithCleanup
WithCleanup[expr,cleanup]
expr を評価し,expr の評価中に,放棄,スロー等が生成された場合でも,結果を返す前に cleanup を実行する.
WithCleanup[init,expr,cleanup]
expr の評価の前に init を評価し,init と cleanup の両方の放棄やスロー等をブロックする.
詳細
- WithCleanup内の init と cleanup では,次のタイプの操作がさまざまな方法でブロックされる.
-
中断 Abortおよびインタラクティブな中断 制約条件 TimeConstrainedやMemoryConstrainedに基づいた終了 コントロールの転送 Throw,Confirm,Goto,Break,Continue … - init の評価中に中断が起動されても,即座にそうなる訳ではなく,expr は評価されないが cleanup は評価される.
- expr の評価中に中断が起動されると,その評価は中断されるが cleanup は実行される.
- cleanup の評価中に中断が起動されても,cleanup の評価が終了するまで延期される.
- TimeConstrained[... WithCleanup[...]... ]等の構築によって外部的に生成された制約は,WithCleanupの中では中断のように振る舞う
- WithCleanup[...]の外部へのコントロールの転送が init あるいは expr の中で起こると,init あるいは expr の評価が終了され,cleanup が評価され,その後で転送が行われる.
- コントロールの転送は,cleanup の評価中に起こった場合は延期されない.
例題
すべて開く すべて閉じる例 (2)
スコープ (2)
f[WithCleanup[Abort[], Echo[1]]]f[WithCleanup[Echo[1];Abort[];Echo[2], Echo[3], Echo[4]]]メインの式の評価中に外部制約に達してもクリーンアップは行われる:
TimeConstrained[WithCleanup[Echo[1], Pause[5];Echo[2], Echo[3]], 1]MemoryConstrained[WithCleanup[Echo[1], Expand[(x + y)^10];Echo[2], Echo[3]], 1000]WithCleanup内部の制約も特別な扱いは受けない:
WithCleanup[Echo[1], TimeConstrained[Pause[5], 1];Echo[2], Echo[3]]アプリケーション (1)
計算が中断された場合でも,作業ファイルが確実に適切に閉じられるようにする:
TimeConstrained[WithCleanup[file = CreateFile[], Do[Pause[.9];Write[file, i], {i, Infinity}], Close[file]], 3]FilePrint[file]同様に,計算がどのように終っても一時ファイルが確実に削除されるようにする:
TimeConstrained[WithCleanup[Do[Pause[.9];Write[file, i], {i, Infinity}], Close[file];DeleteFile[file]], 3]FileExistsQ[file]特性と関係 (3)
初期化またはクリーンアップの最中に中断されても,これらの評価の終了を妨げはしない:
WithCleanup[Abort[];Print[init], Print[main], Print[cleanup]]WithCleanup[Print[init], Print[main], Abort[];Print[cleanup]]WithCleanup[Print[init], Abort[];Print[main], Print[cleanup]]任意の引数での外部制御転送は,その引数の評価を即座に終了する:
WithCleanup[Print["before throw"];Throw[end];Print["after throw"], Print[main], Print[cleanup]]//Catch転送が前の引数で開始された場合はクリーンアップの評価が行われるので注意のこと:
WithCleanup[Print[init], Print["before throw"];Throw[end];Print["after throw"], Print[cleanup]]//CatchWithCleanup[Print[init], Print[main], Print["before throw"];Throw[end];Print["after throw"]]//Catch初期化とクリーンアップは,事実上,AbortProtectで包み込まれる:
Print[1];AbortProtect[Abort[];Print[2]]WithCleanup[Print[1], Abort[];Print[2]]関連するガイド
-
▪
- フロー制御 ▪
- パッケージの保護 ▪
- パッケージの開発 ▪
- ロバスト性とエラー処理 ▪
- スコープ構文
テキスト
Wolfram Research (2020), WithCleanup, Wolfram言語関数, 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_2026_withcleanup, author="Wolfram Research", title="{WithCleanup}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/WithCleanup.html}", note=[Accessed: 25-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_withcleanup, organization={Wolfram Research}, title={WithCleanup}, year={2020}, url={https://reference.wolfram.com/language/ref/WithCleanup.html}, note=[Accessed: 25-June-2026]}