How to catch EnvironmentError Exception in Python?

PythonServer Side ProgrammingProgramming

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.

Example

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

Output

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



raja
Published on 06-Dec-2017 12:56:13
Advertisements