Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How to exit a package in the middle, for example,

BeginPackage["myPackage"]
Begin["`Private"]

DoSomething

If[$FrontEnd===Null, Abort[]]

DoSomethingIfFrontEndDefined

End[]
EndPackage[]

Here Abort[] doesn't really quit the whole package. Is there any command which can do this?

I could use Goto but that sounds not good. Also I don't want to Quit[] because I want to use the definitions in the DoSomething part.

share|improve this question
1  
What's wrong with If[$FrontEnd=!=Null,DoSomethingIfFrontEndDefined] ? And you are missing some trailing backquotes in your context names... –  Albert Retey Jan 18 at 23:09

1 Answer 1

up vote 5 down vote accepted

Rather than $FrontEnd === Null, a better way to test for the existence of a front end is $Notebooks which is a boolean variable:

Kernel:

enter image description here

FrontEnd:

enter image description here

Coming to the crux of your question, your usage of If is a bit confusing... why are you not using the "else" branch? If you set up the conditions right, you can just place all your code in the "then" branch and leave it at that (assuming you don't want to make definitions if there's no front end):

BeginPackage["myPackage`"]
Begin["`Private"]

(* Do something *)
If[$Notebooks, (* Do something else if FE present *)]

(* Do something more *) 

End[]
EndPackage[]
share|improve this answer

Your Answer

 
discard

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.