Question:

Can a single try block have multiple except clauses in Python?

Show Hint

When chaining multiple except clauses:
Always place specific subclass exceptions (e.g., ZeroDivisionError) above more generic exception classes (e.g., Exception) to ensure the specialized handler is matched first!
Updated On: Jun 11, 2026
  • Yes, to catch different types of exceptions
  • No, only one exception type is allowed per block
  • Yes, but only if they are nested
  • No, exceptions must be handled separately
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation


Step 1: Understanding the Question:

The question asks about the rules of exception handling in Python, specifically whether a single try block is allowed to be associated with multiple except blocks to handle different errors.

Step 2: Syntax of Python Exception Handling:

Python uses a structured block system to capture and manage runtime errors:
- A single try block contains the code that could potentially fail.
- It can be followed by multiple sequential except blocks, each handling a different type of exception class.

Step 3: Detailed Explanation:

- When code in a try block is executed and an error occurs, Python stops running the try block immediately and searches the subsequent except blocks from top to bottom.
- By specifying multiple except blocks, we can write specialized recovery code depending on the exact error that occurred. For example:
try:
num = 10 zero
except ZeroDivisionError:
print("Cannot divide by zero!")
except NameError:
print("Variable is not defined!")
- This ensures that if a division by zero occurs, the first handler is run; if an undefined variable is accessed, the second handler is run.
- This structure is fully supported, standard, and highly encouraged in Python development. Thus, Option (A) is correct.

Step 4: Final Answer:

A single try block can indeed have multiple except clauses to catch and handle different types of exceptions separately.
Hence, option (A) is the correct choice.
Was this answer helpful?
0
0