# How to exit a package gracefully in the middle?

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. - 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

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

### FrontEnd:

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[]
`
-