I don't get why sys.exit(1) doesn't exit the while loop in the follow case

Nobody nobody at nowhere.com
Tue Oct 5 16:45:12 EDT 2010


On Tue, 05 Oct 2010 13:57:11 +1100, Ben Finney wrote:

> Here's your problem. Don't ever use a bare ‘except’ unless you know
> exactly why you're doing so. Rather, figure out what exception types you
> want to catch, and catch *only* those types.

If I use a bare except, I usually have a good reason, namely that the
documentation doesn't actually mention which exceptions can be raised.
This is usually because the code doesn't actually know which exceptions
can be raised.

If a module defines:

	def foo(f):
	    f.bar()
	    ...

the set of exceptions which foo() can raise is limitless. The user can
pass whatever they like as "f", and its bar() method can raise anything.

Knowing which exceptions a function or method can raise is the exception
rather than the rule.

If I'm catching exceptions in order to perform clean-up, I'll use a bare
except and re-raise the exception afterwards. In that situation, a bare
except is usually the right thing to do.

If I'm not going to re-raise the exception, I'll either catch Exception or
add explicit catches for SystemExit and KeyboardInterrupt which re-raise
the exception.




More information about the Python-list mailing list