I think Internal`WithLocalSettings
is the tool you want to use. The syntax is:
Internal`WithLocalSettings[
preliminaryCode, (* can't be aborted *)
body, (* can be aborted *)
postprocessCode (* can't be aborted *)
]
Suppose your code looks like:
code[] := (a=1; b=2; Pause[Infinity]; c=3)
and you're interested in the values of a
, b
and c
. Then:
Internal`WithLocalSettings[
Null,
code[],
Print[{a, b, c}]
]
{1,2,c}
$Aborted[]
where I used Evaluation | Abort Evaluation
(actually, the shortcut Cmd + .). Note that code[]
got aborted, yet the Print
statement still occurred, where I use Print
as a substitute for some sort of save file utility.
CheckAbort
andCatch
. $\endgroup$ – Henrik Schumacher Jan 30 '18 at 0:13OpenAppend
and friends. $\endgroup$ – Henrik Schumacher Jan 30 '18 at 0:25AbortProtect
? $\endgroup$ – m_goldberg Jan 30 '18 at 0:52