Stop a script. Just stop it
How can I stop a mathematica script?
Abort[]
does not stop the script, just jump over a number of lines and proceed with next lines of calculations.
Also, killing the kernel seems not good idea. All I want is C++ exit(0); or Java's System.exit(0);.
(* 10000 lines of calculations *)
If[a==5,Print["Calculation gives an unacceptable result. I stop execution here"];Abort[]]
(* 10000 lines of calculations *)
The script invoked from Mathematica GUI with Shift+Enter.
Solution as stated by thorimur
Catch[
(* COMPLETE SCRIPT *)
(* Lots of calculations *)
Throw["Calculation must be stopped"];
Print["Never printed"];
(* Lots of calculations *)
]
Answers 1
While you can always abort a running evaluation manually with Command +
.
(or Ctrl +.
), here are several ways to do this in Mathematica within a given script! It depends on your particular application and setting. The first two are more general than the rest. I'll run through (most of) the possibilities.Confirm
andEnclose
are useful for specific control.Confirm[expr]
checks thatexpr
is not a failure (i.e. is not the value$Failed
or aFailure
, as I understand it, and exits to the nearestEnclose
. Otherwise it returnsexpr
. (You can also include a tag as an argument and exit to the nearestEnclose
with that tag; see the docs.)Enclose
returns aFailure
if an enclosedConfirm
gives it one; otherwise it leaves the expression alone. More specific variants areConfirmBy[expr, f]
andConfirmMatch[expr, patt]
, which work the same way, but in the first case, exits toEnclose
only iff[expr]
doesn't giveTrue
, and in the second, exits toEnclose
only ifexpr
doesn't match the patternpatt
.Throw
andCatch
are similar, butThrow[x]
always exits to the nearest enclosingCatch
, which then returnsx
. So, you'd probably wantThrow
to be in a branch of anIf
or something like that. If noThrow
is encountered,Catch[expr]
returnsexpr
just likeEnclose
. You can also add a tag toThrow
andCatch
like withConfirm
, to control whichCatch
catches whichThrow
.Just for completeness,
Abort[]
andInterrupt[]
should immediately grind the computation to a halt, just as if you'd pressed Command +.
. You can also useAbort[]
like aThrow
, though, by enclosing it withCheckAbort[expr, failexpr]
; ifexpr
produces anAbort[]
,CheckAbort[expr, failexpr]
will evaluate tofailexpr
instead.If you want to end a loop (
Do
,For
, orWhile
) you can useBreak[]
inside it. This just ends the nearest enclosing loop. If you want to simply move on to the next iteration within the loop immediately, but keep the loop going, you can useContinue[]
.If you want to exit a function definition, you can (well, sometimes) use
Return[expr]
. This immediately returnsexpr
, but it can be finicky. For example, you might expectf[x_] := 1 + Return[5]
to return5
as soon as it encounters it, but instead it returns1 + Return[5]
—not even6
. I'd recommendThrow
andCatch
for flow control in general instead.If you really want, you can embrace spaghetti and use
Goto[tag]
andLabel[tag]
in, which work almost exactly how you'd expect, with the caveat thatGoto[tag]
andLabel[tag]
have to be at least nested within some common top level compound expression (expression constructed by;
). So, for example,{(Goto[tag]; 1), (Label[tag]; Print[3]; 4)}
won't work, butIf[True, If[True, Goto[tag]]]; Print[1]; Label[tag]; Print[2]
will.