Challenge - 5 Problems
Exception Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of specific exception handling
What is the output of this code when the input is 0?
Python
try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero") except Exception: print("Some other error")
Attempts:
2 left
💡 Hint
Think about which exception is raised by dividing by zero.
✗ Incorrect
Dividing by zero raises a ZeroDivisionError, so the first except block runs and prints "Cannot divide by zero".
❓ Predict Output
intermediate2:00remaining
Output when catching multiple exceptions
What will this code print?
Python
try: x = int('abc') except (ValueError, TypeError): print("Caught ValueError or TypeError") except Exception: print("Caught some other exception")
Attempts:
2 left
💡 Hint
What exception does int('abc') raise?
✗ Incorrect
int('abc') raises a ValueError, which is caught by the first except block.
❓ Predict Output
advanced2:00remaining
Exception handling with else and finally
What is the output of this code?
Python
try: print("Start") x = 5 / 1 except ZeroDivisionError: print("Divide by zero error") else: print("No error occurred") finally: print("Always runs")
Attempts:
2 left
💡 Hint
Consider what happens when no exception is raised.
✗ Incorrect
Since 5/1 does not raise an error, the else block runs after try, then finally always runs.
❓ Predict Output
advanced2:00remaining
What error is raised?
What error does this code raise?
Python
try: d = {'a': 1} print(d['b']) except KeyError: print("Key not found") except Exception: print("Other error")
Attempts:
2 left
💡 Hint
What happens when you access a missing key in a dictionary?
✗ Incorrect
Accessing d['b'] raises a KeyError, which is caught by the first except block.
❓ Predict Output
expert2:00remaining
Output with nested try-except and re-raising
What is the output of this code?
Python
try: try: x = 1 / 0 except ZeroDivisionError: print("Inner catch") raise except ZeroDivisionError: print("Outer catch")
Attempts:
2 left
💡 Hint
What happens when an exception is re-raised inside an except block?
✗ Incorrect
The inner except catches and prints "Inner catch", then re-raises the exception. The outer except catches it and prints "Outer catch".