I would like a notebook evaluation to entirely stop when an Assert
fails, without quitting the kernel. Is this possible (e.g., by setting $AssertFunction
to ... something)?
Edit:
For example, if I create a notebook containing a cell with the following lines, I would like the value of test
to still be 0
, and for no subsequent cells to be exectuted.
test = 0;
Assert[False];
test = 1;
EDIT:
Note that setting $AssertFunction=Abort[]&
does not work, because it only aborts the current computation, but afterwards evaluation continues to proceed through the notebook.
A related question: why must we execute On[Assert]
before we can set $AssertFunction
? Naturally I understand why we must turn assertions on and off; that is not my question. My question is why On[Assert]
apparently resets $AssertFunction
:
On[Assert]
$AssertFunction = Abort[] &;
Assert[False] (* aborts *)
Off[Assert]
On[Assert]
Assert[False] (* does not abort *)
Abort[]
aborts the current evaluation. You are evaluating a whole notebook at once. That's different: the front end will queue up all inputs and send them one by one to the kernel.Abort[]
only aborts one of these, as it cannot affect the front end state.Abort[]
is only for the kernel. I thought$AssertFunction = FrontEndTokenExecute["EvaluatorAbort"] &
would work but it doesn't. I suggest editing the question and spelling out what I said above to attract more attention (i.e. spelling out thatAbort
doesn't work). $\endgroup$On[Assert]
is so that we can easily turn on and off the assertions. It's like-DNDEBUG
when compiling C code. $\endgroup$SelectionMove
. $\endgroup$