- Trending Categories
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 StopIteration Exception in Python?
When an iterator is done, it’s next method raises StopIteration. This exception is not considered an error.
We re-write the given code as follows to catch the exception and know its type.
Example
import sys try: z = [5, 9, 7] i = iter(z) print i print i.next() print i.next() print i.next() print i.next() except Exception as e: print e print sys.exc_type
Output
<listiterator object at 0x0000000002AF23C8> 5 9 7 <type 'exceptions.StopIteration'>
Advertisements