Challenge - 5 Problems
Exception Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of generic exception handling with division
What is the output of this code snippet?
Python
try: result = 10 / 0 except Exception: print("Error caught") else: print("No error") finally: print("Done")
Attempts:
2 left
💡 Hint
Remember that dividing by zero raises an exception caught by the generic except block.
✗ Incorrect
The division by zero raises an exception. The generic except block catches it and prints 'Error caught'. The finally block always runs and prints 'Done'.
❓ Predict Output
intermediate2:00remaining
Output when no exception occurs in generic handler
What will this code print?
Python
try: x = 5 + 5 except Exception: print("Error") else: print("Success") finally: print("Always")
Attempts:
2 left
💡 Hint
No error happens, so else block runs.
✗ Incorrect
Since no exception occurs, the else block prints 'Success'. The finally block always runs and prints 'Always'.
❓ Predict Output
advanced2:00remaining
Output of nested generic exception handling
What is the output of this nested try-except code?
Python
try: try: print(1/0) except Exception: print("Inner error") raise except Exception: print("Outer error")
Attempts:
2 left
💡 Hint
The inner except re-raises the exception, caught by outer except.
✗ Incorrect
The inner except catches the ZeroDivisionError and prints 'Inner error'. Then it re-raises the exception. The outer except catches it and prints 'Outer error'.
❓ Predict Output
advanced2:00remaining
Output when generic except block is missing
What happens when this code runs?
Python
try: print(5/0) except ValueError: print("Value error")
Attempts:
2 left
💡 Hint
Only ValueError is caught, but division by zero raises ZeroDivisionError.
✗ Incorrect
The except block only catches ValueError, but the code raises ZeroDivisionError which is not caught, so the exception propagates and causes a runtime error.
🧠 Conceptual
expert2:00remaining
Why use generic exception handling?
Which of these is the best reason to use a generic exception handler (except Exception) in your code?
Attempts:
2 left
💡 Hint
Think about what exceptions are subclasses of Exception and which are not.
✗ Incorrect
Using except Exception catches most runtime errors allowing the program to handle them gracefully. It does not catch system-exiting exceptions like KeyboardInterrupt or SystemExit, which is usually desired.