加载中...

14.6 处理多个异常


问题

You have a piece of code that can throw any of several different exceptions, and youneed to account for all of the potential exceptions that could be raised without creatingduplicate code or long, meandering code passages.

解决方案

If you can handle different exceptions all using a single block of code, they can begrouped together in a tuple like this:

try:client_obj.get_url(url)except (URLError, ValueError, SocketTimeout):client_obj.remove_url(url)
In the preceding example, the remove_url() method will be called if any one of thelisted exceptions occurs. If, on the other hand, you need to handle one of the exceptionsdifferently, put it into its own except clause:

try:client_obj.get_url(url)except (URLError, ValueError):client_obj.remove_url(url)except SocketTimeout:client_obj.handle_url_timeout(url)
Many exceptions are grouped into an inheritance hierarchy. For such exceptions, youcan catch all of them by simply specifying a base class. For example, instead of writingcode like this:

try:f = open(filename)except (FileNotFoundError, PermissionError):...
you could rewrite the except statement as:

try:f = open(filename)except OSError:...
This works because OSError is a base class that’s common to both the FileNotFoundErrorand PermissionError exceptions.

讨论

Although it’s not specific to handling multiple exceptions per se, it’s worth noting thatyou can get a handle to the thrown exception using the as keyword:

try:f = open(filename)except OSError as e:if e.errno == errno.ENOENT:logger.error(‘File not found')elif e.errno == errno.EACCES:logger.error(‘Permission denied')else:logger.error(‘Unexpected error: %d', e.errno)
In this example, the e variable holds an instance of the raised OSError. This is useful ifyou need to inspect the exception further, such as processing it based on the value of anadditional status code.Be aware that except clauses are checked in the order listed and that the first matchexecutes. It may be a bit pathological, but you can easily create situations where multipleexcept clauses might match. For example:

>>> f = open('missing')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'missing'
>>> try:
...     f = open('missing')
... except OSError:
...     print('It failed')
... except FileNotFoundError:
...     print('File not found')
...
It failed
>>>

Here the except FileNotFoundError clause doesn’t execute because the OSError ismore general, matches the FileNotFoundError exception, and was listed first.As a debugging tip, if you’re not entirely sure about the class hierarchy of a particularexception, you can quickly view it by inspecting the exception’s mro attribute. Forexample:

>>> FileNotFoundError.__mro__
(<class 'FileNotFoundError'>, <class 'OSError'>, <class 'Exception'>,
 <class 'BaseException'>, <class 'object'>)
>>>

Any one of the listed classes up to BaseException can be used with the except statement.


还没有评论.