
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Example code
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>)
- Related Articles
- 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 LIKE clause to fetch multiple values beginning with “Johâ€
- How to use MySQL DISTINCT clause on multiple columns?
- How do you handle an exception thrown by an except clause in Python?
- How to use multiple modules with Python import Statement?
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- How to use the try-finally clause to handle exception in Python?
- How can I write a try/except block that catches all Python exceptions?
- How to use MySQL VIEW with WHERE clause?
- How to use Custom Exceptions in JavaScript?
- How to use MySQL Date functions with WHERE clause?
- Update with multiple values in MySQL WHERE clause
- What MySQL returns when we use DISTINCT clause with the column having multiple NULL values?
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?

Advertisements