Challenge - 5 Problems
Exception Chain Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of chained exception test with pytest
What is the output of this pytest test when run?
PyTest
import pytest def func(): try: raise ValueError("Initial error") except ValueError as e: raise RuntimeError("Chained error") from e def test_exception_chain(): with pytest.raises(RuntimeError) as excinfo: func() assert isinstance(excinfo.value.__cause__, ValueError) assert str(excinfo.value) == "Chained error"
Attempts:
2 left
💡 Hint
Remember that 'raise ... from ...' sets the __cause__ attribute of the exception.
✗ Incorrect
The test checks that the RuntimeError was raised and that its __cause__ is the original ValueError. The assertions match the expected exception chain, so the test passes.
❓ assertion
intermediate1:30remaining
Correct assertion to verify exception cause in pytest
Which assertion correctly verifies that a caught exception has a cause of type KeyError?
PyTest
import pytest with pytest.raises(RuntimeError) as excinfo: raise RuntimeError("Error") from KeyError("missing key")
Attempts:
2 left
💡 Hint
The __cause__ attribute holds the original exception instance, not the class.
✗ Incorrect
Option A correctly checks that the __cause__ attribute is an instance of KeyError. Option A compares the instance to the class, which is false. Option A checks args incorrectly. Option A is unrelated.
🔧 Debug
advanced2:00remaining
Why does this pytest test fail to detect the cause exception?
Given this test code, why does the assertion fail?
PyTest
import pytest def func(): try: raise IndexError("Index problem") except IndexError: raise RuntimeError("Runtime problem") def test_chain(): with pytest.raises(RuntimeError) as excinfo: func() assert excinfo.value.__cause__ is not None
Attempts:
2 left
💡 Hint
Check how exception chaining works when 'from' is omitted.
✗ Incorrect
When raising a new exception without 'from', Python sets __cause__ to None. The original exception is stored in __context__, but __cause__ remains None, so the assertion fails.
🧠 Conceptual
advanced1:30remaining
Understanding exception chaining attributes
Which statement about Python exception chaining is TRUE?
Attempts:
2 left
💡 Hint
Think about how explicit and implicit chaining differ.
✗ Incorrect
Option D is correct: __cause__ is set explicitly by 'raise ... from ...'. __context__ is set automatically when an exception occurs during handling another exception. Other options are false.
❓ framework
expert2:30remaining
Testing nested exception chains with pytest
Consider this nested exception chain. Which pytest assertion correctly verifies the root cause exception type?
PyTest
import pytest def nested_func(): try: raise KeyError("key missing") except KeyError as e: raise ValueError("value error") from e def test_nested_chain(): with pytest.raises(ValueError) as excinfo: nested_func() # Which assertion below correctly checks the root cause is KeyError?
Attempts:
2 left
💡 Hint
Check the chain depth and which exception is caught by pytest.raises.
✗ Incorrect
The ValueError is raised from KeyError, so excinfo.value.__cause__ is the KeyError instance. __cause__.__cause__ is None. Option B correctly checks the immediate cause.