Challenge - 5 Problems
Multiple Exception Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of multiple except blocks
What is the output of this Python code with multiple except blocks?
Python
try: x = int('abc') except ValueError: print('ValueError caught') except Exception: print('General exception caught')
Attempts:
2 left
💡 Hint
The code tries to convert a string that cannot be converted to int.
✗ Incorrect
The int('abc') raises a ValueError, so the first except block catches it and prints 'ValueError caught'.
❓ Predict Output
intermediate2:00remaining
Which except block runs?
What will be printed when this code runs?
Python
try: result = 10 / 0 except ZeroDivisionError: print('ZeroDivisionError handled') except ArithmeticError: print('ArithmeticError handled')
Attempts:
2 left
💡 Hint
ZeroDivisionError is a subclass of ArithmeticError.
✗ Incorrect
The ZeroDivisionError is raised and caught by the first matching except block, so only 'ZeroDivisionError handled' is printed.
❓ Predict Output
advanced2:00remaining
Output with multiple exceptions in one except
What is the output of this code?
Python
try: x = int('10a') except (ValueError, TypeError): print('Caught ValueError or TypeError') except Exception: print('Caught other Exception')
Attempts:
2 left
💡 Hint
int('10a') raises a ValueError.
✗ Incorrect
The ValueError is caught by the except block that handles both ValueError and TypeError.
❓ Predict Output
advanced2:00remaining
Exception hierarchy and except order
What will this code print?
Python
try: {}['key'] except KeyError: print('KeyError caught') except LookupError: print('LookupError caught')
Attempts:
2 left
💡 Hint
KeyError is a subclass of LookupError.
✗ Incorrect
The KeyError is raised and caught by the first except block that matches it exactly.
❓ Predict Output
expert2:00remaining
Output with nested try-except and multiple exceptions
What is the output of this nested try-except code?
Python
try: try: x = 1 / 0 except TypeError: print('Inner TypeError') except ZeroDivisionError: print('Outer ZeroDivisionError')
Attempts:
2 left
💡 Hint
The inner except does not catch ZeroDivisionError, so it propagates to outer except.
✗ Incorrect
The ZeroDivisionError is not caught by the inner except, so it is caught by the outer except block.