- 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 IndexError Exception in Python?
An IndexError is raised when a sequence reference is out of range.
The given code is rewritten as follows to catch the exception and find its type
Example
import sys try: my_list = [3,7, 9, 4, 6] print my_list[6] except IndexError as e: print e print sys.exc_type
Output
C:/Users/TutorialsPoint1~.py list index out of range <type 'exceptions.IndexError'>
Advertisements
To Continue Learning Please Login
Login with Google