0
0
Pythonprogramming~20 mins

Exception chaining in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of chained exceptions with raise from
What is the output of this Python code when executed?
Python
def func():
    try:
        x = 1 / 0
    except ZeroDivisionError as e:
        raise ValueError("Invalid value") from e

try:
    func()
except Exception as ex:
    print(type(ex).__name__)
    print(ex)
    print(type(ex.__cause__).__name__)
    print(ex.__cause__)
A
ValueError
Invalid value
ZeroDivisionError
division by zero
B
ZeroDivisionError
division by zero
ValueError
Invalid value
C
ValueError
Invalid value
None
None
D
ZeroDivisionError
Invalid value
ValueError
division by zero
Attempts:
2 left
💡 Hint
Remember that 'raise ... from ...' sets the __cause__ attribute of the new exception.
Predict Output
intermediate
2:00remaining
Effect of exception chaining without 'from'
What will be printed by this code?
Python
try:
    try:
        int('abc')
    except ValueError:
        raise RuntimeError('Conversion failed')
except Exception as e:
    print(type(e).__name__)
    print(e)
    print(e.__cause__)
A
RuntimeError
Conversion failed
ValueError('invalid literal for int() with base 10: \'abc\'')
B
ValueError
invalid literal for int() with base 10: 'abc'
None
C
RuntimeError
Conversion failed
None
D
RuntimeError
Conversion failed
RuntimeError
Attempts:
2 left
💡 Hint
Without 'from', the __cause__ attribute is not set automatically.
🔧 Debug
advanced
2:00remaining
Identify the error in exception chaining syntax
Which option contains a syntax error related to exception chaining?
A
try:
    1/0
except ZeroDivisionError as e:
    raise ValueError('Error') from e
B
try:
    1/0
except ZeroDivisionError as e:
    raise ValueError('Error') with e
C
e morf )'rorrE'(rorrEeulaV esiar    
:e sa rorrEnoisiviDoreZ tpecxe
0/1    
:yrt
D
try:
    1/0
except ZeroDivisionError as e:
    raise ValueError('Error')
Attempts:
2 left
💡 Hint
Check the keyword used to chain exceptions.
Predict Output
advanced
2:00remaining
Output of nested exception chaining with __context__
What will this code print?
Python
def f():
    try:
        int('a')
    except ValueError:
        try:
            1/0
        except ZeroDivisionError:
            raise RuntimeError('Runtime problem')

try:
    f()
except Exception as e:
    print(type(e).__name__)
    print(e)
    print(type(e.__context__).__name__)
    print(e.__context__)
A
ZeroDivisionError
division by zero
ValueError
invalid literal for int() with base 10: 'a'
B
RuntimeError
Runtime problem
ValueError
invalid literal for int() with base 10: 'a'
C
RuntimeError
Runtime problem
None
None
D
RuntimeError
Runtime problem
ZeroDivisionError
division by zero
Attempts:
2 left
💡 Hint
When an exception is raised during handling another, __context__ stores the original exception.
🧠 Conceptual
expert
2:00remaining
Understanding implicit vs explicit exception chaining
Which statement best describes the difference between implicit and explicit exception chaining in Python?
AExplicit chaining uses 'raise ... from ...' to set __cause__, while implicit chaining sets __context__ automatically when an exception occurs during handling another.
BImplicit chaining uses 'raise ... from ...' to set __cause__, while explicit chaining sets __context__ automatically.
CExplicit chaining sets __context__ to None, implicit chaining sets __cause__ to None.
DImplicit chaining requires manual assignment of __cause__, explicit chaining happens automatically.
Attempts:
2 left
💡 Hint
Think about how Python links exceptions automatically versus when you use 'from'.