There is very little difference between the two types. In fact, even the core Python developers agreed that there is no real difference and removed IOError in Python 3 (it is now an alias for OSError). See PEP 3151 - Reworking the OS and IO exception hierarchy:
While some of these distinctions can be explained by implementation considerations, they are often not very logical at a higher level. The line separating
OSErrorandIOError, for example, is often blurry. Consider the following:>>> os.remove("fff") Traceback (most recent call last): File "<stdin>", line 1, in <module> OSError: [Errno 2] No such file or directory: 'fff' >>> open("fff") Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 2] No such file or directory: 'fff'
Yes, that's two different exception types with the exact same error message.
For your own code, stick to throwing OSError. For existing functions, check the documentation (it should detail what you need to catch), but you can safely catch both:
try:
# ...
except (IOError, OSError):
# handle error
Quoting the PEP again:
Answer from Martijn Pieters on Stack OverflowIn fact, it is hard to think of any situation where
OSErrorshould be caught but notIOError, or the reverse.
There is very little difference between the two types. In fact, even the core Python developers agreed that there is no real difference and removed IOError in Python 3 (it is now an alias for OSError). See PEP 3151 - Reworking the OS and IO exception hierarchy:
While some of these distinctions can be explained by implementation considerations, they are often not very logical at a higher level. The line separating
OSErrorandIOError, for example, is often blurry. Consider the following:>>> os.remove("fff") Traceback (most recent call last): File "<stdin>", line 1, in <module> OSError: [Errno 2] No such file or directory: 'fff' >>> open("fff") Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 2] No such file or directory: 'fff'
Yes, that's two different exception types with the exact same error message.
For your own code, stick to throwing OSError. For existing functions, check the documentation (it should detail what you need to catch), but you can safely catch both:
try:
# ...
except (IOError, OSError):
# handle error
Quoting the PEP again:
In fact, it is hard to think of any situation where
OSErrorshould be caught but notIOError, or the reverse.
There's no difference between IOError and OSError cause they mostly appear on similar commands like opening a file or removing one.
load_file: OSError vs IOError
How to differentiate different IOErrors?
Better communicate that `IOError` and `WindowsError` are just aliases of `OSError` now
python - Dealing with I/O exceptions beyond OSError - Stack Overflow
I was going through documentation of Python learning about Exceptions.
On this page of documentation https://docs.python.org/3/library/exceptions.html#Exception I found EnvironmentError, IOError but there is not any text defining these.
Can anybody here explain these two?
Thanks