
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How to catch IOError Exception in Python?
IOError Exception
It is an error raised when an input/output operation fails, such as the print statement or the open() function when trying to open a file that does not exist. It is also raised for operating system-related errors.
If the given code is written in a try block, it raises an input/output exception, which is handled in the except block as shown given below
Example
import sys def whatever(): try: f = open ( "foo.txt", 'r' ) except IOError, e: print e print sys.exc_type whatever()
Output
[Errno 2] No such file or directory: 'foo.txt' <type 'exceptions.IOError'>
- Related Articles
- How to catch KeyError Exception in Python?
- How to catch ArithmeticError Exception in Python?
- How to catch OverflowError Exception in Python?
- How to catch IndexError Exception in Python?
- How to catch NameError Exception in Python?
- How to catch EOFError Exception in Python?
- How to catch SyntaxError Exception in Python?
- How to catch IndentationError Exception in python?
- How to catch TypeError Exception in Python?
- How to catch EnvironmentError Exception in Python?
- How to catch LookupError Exception in Python?
- How to catch ZeroDivisionError Exception in Python?
- How to catch FloatingPointError Exception in Python?
- How to catch StopIteration Exception in Python?
- How to catch SystemExit Exception in Python?

Advertisements