References to come: PythonCookbook
First of all you know, all the abnormal things a class.
If a single block of code to handle all the different anomalies, they can be grouped into one of the tuples.
from urllib.error import URLError try: object.get_url(url) except (URLError, ValueError, SocketTimeout): object.remove_url(url)
This error Ganso (URLError, ValueError, SocketTimeout) to capture will be executed object.remove_url (url)
If a single take a different approach, it can be placed in a separate except
try: object.get_url(url) except (URLError, ValueError): object.remove_url(url) except SocketTimeout: object.other_do(url)
There are many grouped into the inheritance hierarchy, for such an exception, you can catch all exceptions by specifying a base class.
try: f = open('a.txt') except (FileNotFoundError, PermissionError): ... try: f = open('a.txt') except OSError: ... # Of such investigation under the following sub-categories print(OSError.__subclasses__()) Print ()
# View table inheritance print(FileNotFoundError.__mro__)
[<class 'ConnectionError'>, <class 'BlockingIOError'>, <class 'ChildProcessError'>, <class 'FileExistsError'>,
<class 'FileNotFoundError'>, <class 'IsADirectoryError'>, <class 'NotADirectoryError'>, <class 'InterruptedError'>,
<class 'PermissionError'>, <class 'ProcessLookupError'>, <class 'TimeoutError'>, <class 'io.UnsupportedOperation'>, <class 'signal.ItimerError'>] (<class 'FileNotFoundError'>, <class 'OSError'>, <class 'Exception'>, <class 'BaseException'>, <class 'object'>)
The book is not rigorous local things, the type of error is more and more obvious OSError included.
We can also as e, the e is captured to the wrong instance, the instance, then they should have a bunch of attributes, you can also view the type of e by type (e) ways to facilitate the accurate capture.
Wherein the error property can capture the error code.
except to execute appropriate exception capture down, except the following will not be executed.
try: f = open('a.txt') except OSError as e:
print(dir(e)) print('OSError') # This certainly will not be executed, should be FileNotFoundError OSError parent class that contains it to all exceptions are captured except FileNotFoundError: print('FileNotFoundError')
At the attributes of objects on the following OSError
['__cause__', '__class__', '__context__', '__delattr__', '__dict__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__',
'__subclasshook__', '__suppress_context__', '__traceback__', 'args', 'characters_written', 'errno', 'filename', 'filename2', 'strerror', 'with_traceback']
Catch all exceptions
try: ... # General error this enough except Exception as e: ... # This is the wrong father except BaseException as e: ...
In addition to SystemExit, KeyboardInterrupt, and GeneratorExit, Exception can capture