- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 use the ‘except’ clause with multiple exceptions in Python?
It is possible to define multiple exceptions with the same except clause. It means that if the Python interpreter finds a matching exception, then it’ll execute the code written under except clause.
In general, the syntax for multiple exceptions is as follows
Except(Exception1, Exception2,…ExceptionN) as e:
When we define except clause in this way, we expect the same code to throw different exceptions. Also, we want to take the action in each case.
import sys try: d = 8 d = d + '5' except(TypeError, SyntaxError)as e: print sys.exc_info()
We get output as shown
(<type 'exceptions.TypeError'>, TypeError("unsupported operand type(s) for +: 'int' and 'str'",), <traceback object at 0x0000000002954748>)
- How to use the ‘except clause’ with No Exceptions in Python?
- How to catch multiple exceptions in one line (except block) in Python?
- How to use MySQL DISTINCT clause on multiple columns?
- How to use MySQL LIKE clause to fetch multiple values beginning with “Joh”
- How to use MySQL VIEW with WHERE clause?
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- How to use multiple modules with Python import Statement?
- How can I write a try/except block that catches all Python exceptions?
- How to use the try-finally clause to handle exception in Python?
- How do you handle an exception thrown by an except clause in Python?
- How to use MySQL Date functions with WHERE clause?
- What MySQL returns when we use DISTINCT clause with the column having multiple NULL values?
- Update with multiple values in MySQL WHERE clause
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?