Halt, stop, quit, exit?

Alex Martelli aleax at aleax.it
Mon Oct 13 10:08:55 EDT 2003


Stephen Horne wrote:

> On Mon, 13 Oct 2003 13:55:19 +0100, Simon Faulkner
> <news at titanic.co.uk> wrote:
> 
>>Does Python have a command that just stops all processing?
> 
> Yes : sys.exit (value)
> 
> See the library docs for details.
> 
> However, IMO this is normally the wrong thing. I would normally raise
> an exception, 

...and that's what sys.exit does on your behalf: it raises the
built-in exception SystemExit.  If you choose to use a raise
statement explicitly, you can then also use your own exception
classes, subclassed from the system-provided ones, for finer
grained control.  Admittedly, however, such needs are typically
rather advanced ones; for many applications, such fine-grained
control may well not be necessary.

> and the outer level of processing would have a try block
> that catches all exceptions (by name for those which can be
> anticipated) reporting details of why the program stopped.
> 
> Should you decide that later that you only want to abort part of your
> app (e.g. to go back to the main menu), an exception can handle this
> quite naturally.

Yes, but you can choose to do that with SystemExit as well, if you
wish.  Stylistically, there is something to be said both for and
against, of course.


Alex





More information about the Python-list mailing list