![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to catch OSError Exception in Python?
OSError serves as the error class for the os module, and is raised when an error comes back from an os-specific function.
We can re-write the given code as follows to handle the exception and know its type.
#foobar.py import os import sys try: for i in range(5): print i, os.ttyname(i) except Exception as e: print e print sys.exc_type
If we run this script at linux terminal
$ python foobar.py
We get the following output
OUTPUT
0 /dev/pts/0 1 /dev/pts/0 2 /dev/pts/0 3 [Errno 9] Bad file descriptor <type 'exceptions.OSError'>
Advertisements