- 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 KeyError Exception in Python?
A KeyError is raised when a value is not found as a key of a dictionary. The given code is rewritten as follows to catch the exception and find its type.
Example
import sys try: s = {'a':5, 'b':7}['c'] except: print (sys.exc_info())
Output
(<type 'exceptions.KeyError'>, KeyError('c',), <traceback object at 0x0000000003203748>)
Advertisements