Question

How can you catch multiple exceptions in one block?

Updated on May 31, 2025 | By Learnzy Admin | πŸ‘οΈ Views: 199 students

Solution
βœ” Verified

You can catch more than one type of error using a single except block by putting the exception names in round brackets.

Syntax:-

try:
    # Code that may give different types of errors
except (ErrorType1, ErrorType2):
    # This block will run if any of the errors happen

Example:--

try:
    a = int("abc")     # This gives ValueError
    b = 10 / 0         # This gives ZeroDivisionError
except (ValueError, ZeroDivisionError):
    print("An error occurred!")

Explanation:

  • If any of the errors (ValueError or ZeroDivisionError) happens, the except block will run.
  • This helps to handle multiple errors together without writing separate except blocks.
Was this solution helpful? 37
Click here to download practice questions on Exception Handling in Python

More Questions on Exception Handling in Python

Question 1

What is the purpose of the else block in exception handling?


View solution
Question 2

What is the use of raise in Python?


View solution
Question 3

What is the use of the finally block?


View solution
Question 4

Write the syntax of exception handling in Python.


View solution
Question 5

Name any four common exceptions in Python.


View solution
Question 6

"Every syntax error is an exception but every exception cannot be a syntax error." Justify the statement.


View solution