3
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Did you also see this and this? $\endgroup$
    – Michael E2
    Dec 13, 2019 at 13:24
  • $\begingroup$ @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 $\endgroup$
    – larry
    Dec 13, 2019 at 14:22

1 Answer 1

3
$\begingroup$

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:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.