Module (57%)
Section (57%)

The original exception object e is now being referenced by the __context__ attribute of the following exception f.

Inner exception (f): division by zero Outer exception (e): list index out of range Outer exception referenced: list index out of range Is it the same object: True

output

The except Exception clause is a wide one and normally should be used as a last resort to catch all unhandled exceptions. It’s so wide because we don’t know what kind of exception might occur.

So, when a subsequent exception (much better forecasted) occurs, we still can say a lot about the nature of the first exception.

Isn't it handled with ease?


Code

a_list = ['First error', 'Second error']

try:
print(a_list[3])
except Exception as e:
try:
# the following line is a developer mistake - they wanted to print progress as 1/10 but wrote 1/0
print(1 / 0)
except ZeroDivisionError as f:
print('Inner exception (f):', f)
print('Outer exception (e):', e)
print('Outer exception referenced:', f.__context__)
print('Is it the same object:', f.__context__ is e)
{{ dockerServerErrorMsg }} ×
{{ errorMsg }} ×
{{ successMsg }} ×