What happens to my first exception (A
) when the second (B
) is raised in the following code?
class A(Exception): pass
class B(Exception): pass
try:
try:
raise A('first')
finally:
raise B('second')
except X as c:
print(c)
If run with X = A
I get:
Traceback (most recent call last): File "raising_more_exceptions.py", line 6, in raise A('first') __main__.A: first During handling of the above exception, another exception occurred: Traceback (most recent call last): File "raising_more_exceptions.py", line 8, in raise B('second') __main__.B: second
But if X = B
I get:
second
Questions
- Where did my first exception go?
- Why is only the outermost exception catchable?
- How do I peel off the outermost exception and reraise the earlier exceptions?
Update0
This question specifically addresses Python 3, as its exception handling is quite different to Python 2.
question from:https://stackoverflow.com/questions/6278426/raising-exceptions-when-an-exception-is-already-present-in-python-3