How to catch EnvironmentError Exception in Python?

EnvironmentError is the base class for errors that come from outside of Python (the operating system, filesystem, etc.). EnvironmentError Exception is a subclass of the StandarError class. It is the base class for IOError and OSError exceptions. It is not actually raised unlike its subclass errors like IOError and OSError.

Any example of an IOError or OSError should be an example of Environment Error as well.


import sys
f = open ( "JohnDoe.txt", 'r' )
except Exception as e:
print e
print sys.exc_type


[Errno 2] No such file or directory: 'JohnDoe.txt'
<type 'exceptions.IOError'>