3

I often slect the whole notebook to evaluate. Sometimes some functions may return wrong parameters, which may happen, for example, I may have forgotten to specify values to some variables in the function, or due to some internal errors such as reading a file failed. When this happens, the subsquent evaluations could take extremely long time, which can't be exited using Alt+/, and I have to Quit the kernal or shut down the softward completely when it is freezing.

Hence, I'd like to stop all the subsquent evaluations once I found an error, like I got an $Failed flag after reading a file. The Abort[] can only skip one execution. I wonder if there is any command to stop all the subsquent evaluations, so that I can use the command to stop the subsquent evaluations when I detect an error flag?

I already seen this, but doesn't work for me on windows 10. The two prints * and ** are both printed in the notebook. Thanks.

larry
  • 725
  • 3
  • 11
  • Did you also see this and this? – Michael E2 Dec 13 '19 at 13:24
  • @MichaelE2, thanks for the comments, the approach by Kuba does work, though it's more complex than I have expected. I wonder if I have to include the definition of the assertfunction in any nb file that need to use it? Or can I embedd it in the functions (within user-defined packages) which needs to use the Assert[]? Alternatively, is there anyway to set it as defaults, so that it takes effect in any notebook, even if I restart the kernel? If so, how can I reset it, and see the list of options that I overwrite? Thanks – larry Dec 13 '19 at 14:22

1 Answers1

3

You might find use in this definition of $Post:

$Post = Function[, 
       Unevaluated[#] /. $Failed :> 
     RuleCondition@FrontEndTokenExecute["EvaluatorAbort"], HoldAllComplete];

With this defined any printed output that contains $Failed should trigger an abort of all subsequent cells.

Reference:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371