Multiple exceptions — Python Technical Interview Questions & Answers
Multiple exceptions
Multiple exceptions
- One except can catch several types. except (TypeError, ValueError) as e: handles both. Or write two except blocks if the recovery is different. Order matters: catch the specific type before a wide parent like Exception, or the wide one steals the case.
- Tuple form vs separate except — both valid. as e gives the message. Dry-run: int("Asha") is ValueError, 1 + "a" is TypeError. Same handler can print e for both.
- Multiple exceptions — output: ValueError. int("Asha") cannot convert.
try: 10 / 0
│ ZeroDivisionError
▼
except → Cannot divide
│
▼
finallyTechnical Interview Tip: Show the tuple syntax once.
Code Examples for Multiple exceptions
Example
# Multiple exceptions
try:
int("Asha")
except (ValueError, TypeError) as e:
print(type(e).__name__)
Multiple exceptions — output: ValueError. int("Asha") cannot convert.
How to Explain Multiple exceptions to an Interviewer
Explain Multiple exceptions as if you are teaching a junior — definition, then one tiny script.
Except (A, B) Or multiple except blocks
What does the example print, and why?
Multiple exceptions — output: ValueError
What mistake do freshers make with Multiple exceptions?
Order: specific first