- 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 ValueError using Exception in Python?
A ValueError is used when a function receives a value that has the right type but an invalid value.
The given code can be rewritten as follows to handle the exception and find its type.
Example
import sys try: n = int('magnolia') except Exception as e: print e print sys.exc_type
Output
invalid literal for int() with base 10: 'magnolia' <type 'exceptions.ValueError'>
Advertisements