0
0
Pythonprogramming~20 mins

Generic exception handling in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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")
ANo error\nDone
BError caught\nDone
CDone
DZeroDivisionError
Attempts:
2 left
💡 Hint
Remember that dividing by zero raises an exception caught by the generic except block.
Predict Output
intermediate
2: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")
ASuccess\nAlways
BError\nAlways
CAlways
D10
Attempts:
2 left
💡 Hint
No error happens, so else block runs.
Predict Output
advanced
2: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")
AInner error\nOuter error
BInner error
COuter error
DZeroDivisionError
Attempts:
2 left
💡 Hint
The inner except re-raises the exception, caught by outer except.
Predict Output
advanced
2:00remaining
Output when generic except block is missing
What happens when this code runs?
Python
try:
    print(5/0)
except ValueError:
    print("Value error")
ANo output
BZeroDivisionError
CZeroDivisionError exception raised
DValue error
Attempts:
2 left
💡 Hint
Only ValueError is caught, but division by zero raises ZeroDivisionError.
🧠 Conceptual
expert
2:00remaining
Why use generic exception handling?
Which of these is the best reason to use a generic exception handler (except Exception) in your code?
ATo improve program speed by avoiding specific exception checks
BTo ignore all errors silently without any notification
CTo catch all errors including system-exiting exceptions like KeyboardInterrupt
DTo catch all exceptions that are subclasses of Exception, allowing graceful error handling
Attempts:
2 left
💡 Hint
Think about what exceptions are subclasses of Exception and which are not.