Halt, stop, quit, exit?

S

Stephen Horne

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

Alex Martelli

Stephen said:
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
 
J

John J. Lee

Alex Martelli said:
...and that's what sys.exit does on your behalf: it raises the
built-in exception SystemExit. If you choose to use a raise
[...]

But if you (Simon) really want to just exit unconditionally, it's

os._exit(code)


John
 
P

Peter Hansen

Simon said:
Does Python have a command that just stops all processing?

Do you want to clean up open file handles and such, including
properly freeing up any held resources, before terminating
the application? Or are you looking for the ugliest, dirtiest,
fastest way out, regardless of the damage it might cause?

Choose between sys.exit() or os._exit() as needed.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top