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.

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'>



Updated on: 12-Feb-2020

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements