0
0
Pythonprogramming~20 mins

Multiple exception handling in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Exception Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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')
AGeneral exception caught
BValueError caught
CNo output
DSyntaxError
Attempts:
2 left
💡 Hint
The code tries to convert a string that cannot be converted to int.
Predict Output
intermediate
2: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')
AZeroDivisionError and ArithmeticError handled
BArithmeticError handled
CZeroDivisionError handled
DNo output
Attempts:
2 left
💡 Hint
ZeroDivisionError is a subclass of ArithmeticError.
Predict Output
advanced
2: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')
ANo output
BCaught other Exception
CSyntaxError
DCaught ValueError or TypeError
Attempts:
2 left
💡 Hint
int('10a') raises a ValueError.
Predict Output
advanced
2:00remaining
Exception hierarchy and except order
What will this code print?
Python
try:
    {}['key']
except KeyError:
    print('KeyError caught')
except LookupError:
    print('LookupError caught')
AKeyError caught
BLookupError caught
CNo output
DTypeError
Attempts:
2 left
💡 Hint
KeyError is a subclass of LookupError.
Predict Output
expert
2: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')
AOuter ZeroDivisionError
BNo output
CInner TypeError
DZeroDivisionError not caught
Attempts:
2 left
💡 Hint
The inner except does not catch ZeroDivisionError, so it propagates to outer except.