Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a relatively simple bit of code that looks like the following:

---some code here---
If[statement, Abort[]];
---more code following---

However, while $Aborted appears as output, the evaluation of the rest of the cell does not abort, continuing execution of code beyond the If statement. How do I actually get the running cell to cease evaluating the rest of the cell?

Edit: It seems that one way to accomplish this is to wrap the entire cell in parenthesis, e.g., but this is a fairly ugly solution, especially with auto-formatting turned on (my cell has many lines of code, and the parenthesis are huuuuuge). Anyone have a more elegant solution?

---some code here---
If[statement, Abort[]];
---more code following---
share|improve this question
up vote 10 down vote accepted

This question has been asked before on stack overflow. However we will summerize some of the answers given there on our new Mathematica site.

Wrap In Compound Expression

One suggestion by Michael Pilat, given there was to wrap your lines in CompoundExpression, e.g.


During evaluation of In[39]:= {1,2,3,4,5}


If you mind the formatting of parenthesis, you could explicitly use the FullForm of CompoundExpression like this:


Use of $PreRead

Suggested by Alexey Popkov:

In[1]:= $new$PreRead = False;
AbortAllPendingInputs := 
  AbortProtect[If[! $new$PreRead, $new$PreRead = True;
    $TimeOfAbort = SessionTime[];
    last$PreRead = ToString[Definition[$PreRead], InputForm];
    $PreRead := If[TrueQ[SessionTime[] - $TimeOfAbort < 1], "",
       $new$PreRead = False;
       If[last$PreRead === "Null", #, 
        ToExpression[last$PreRead]; $PreRead@#]
       ] &;]];

In[3]:= f := CheckAbort[Pause[10], AbortAllPendingInputs; Abort[]]

In[4]:= While[True, f]
While[True, f]
While[True, f]

Out[4]= $Aborted

Note that this solution will need to be modified if you use $PreRead for anything else.

Use of CellEvaluationFunction

CellEvaluationFunction gets the BoxData expression, before it is even split. Just assigning something like CellEvaluationFunction:>ToExpression seems to do the trick.

You can set it as an Input cell style, or as a notebook option, or front end session option, or however you like

SetOptions[$FrontEndSession, CellEvaluationFunction :> ToExpression]

However, you lose the multiple outputs in those new lines when you don't use ;. This can probably be solved but I don't know very well how CellEvalutionFunction works, and how can it receive a box structure, default to Identity but expect to return an expression. Edit should you come up with any improvements

share|improve this answer
Since you tagged this community wiki, would you mind if others were to port over some of the suggestions from the stack overflow question here? With appropriate credit and whatnot of course! – nixeagle Mar 28 '12 at 21:52
@nixeagle No, of course I don't mind! That was the idea. – Leonid Shifrin Mar 28 '12 at 21:55
Alright, I'll go ahead and work on that. Plus I ought to suggest to @Guillochon that using more smaller functions might help with the concern raised in his Edit. – nixeagle Mar 28 '12 at 21:56
Alright, done editing this. If anyone else can make the answer clearer or wants to volunteer a 3rd (possibly better!) solution, please do so. As a side note, as I don't know where else to mention this, do we want to keep that abort tag on this question? – nixeagle Mar 28 '12 at 22:06
@LeonidShifrin, I just saw that your answer was a community wiki and that in the link you provided someone suggests in comments a CellEvaluationFunction approach, like my answer. Should I delete it and put it in the community answer? – Rojo Mar 31 '12 at 18:23

Note that the documentation FrontEnd uses CellEvaluationFunction -> Identity and not ToExpression. Still the example works and

Print[a]; Abort[]; Print[b]

aborts properly (not printing b) because all statements are in one line. Apparently it is the line breaks which break up the cell into individual commands, not the semicolons. So there must be another way "fixing" this.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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