Dealing with exceptions

Nobody nobody at nowhere.com
Mon Mar 4 00:01:48 CET 2013


On Sat, 02 Mar 2013 18:52:19 +0100, Kwpolska wrote:

> Also, you can do `except:` for a catch-all, but it is discouraged unless
> you have REALLY good reasons to do this.  And, most of the time, you
> don’t.

Most of the time you probably want to catch either Exception (which
excludes GeneratorExit, KeyboardInterrupt and SystemExit) or StandardError
(which excludes the above pluse warnings and StopIteration).

Only in specific circumstances can you reasonably go finer than that,
partly due to the set of exceptions a method may throw seldom being
documented, and partly due to duck typing; a parameter which is intended
to be a file object could realistically be any object which supports the
appropriate methods (e.g. .read()), and there's no guarantee that those
methods will have the same set of possible exceptions as a real file
object.




More information about the Python-list mailing list