Skip to main content
edited tags
Link
edited tags
Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751
added 518 characters in body
Source Link
Alan
  • 13.8k
  • 21
  • 41

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;

An additionalEDIT:

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 *)

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;

An additional question: why must we execute On[Assert] before we can set $AssertFunction?

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 *)
Tweeted twitter.com/StackMma/status/664250236508983296
provide example to clarify question
Source Link
Alan
  • 13.8k
  • 21
  • 41
Loading
edited tags
Link
Loading
Source Link
Alan
  • 13.8k
  • 21
  • 41
Loading