circuitpython/tests/basics/exception_chain.py
Jeff Epler f3169246ba
Implement chained exceptions
This adds the __cause__, __context__ and __suppress_context__
members to exception objects and makes e.g., `raise exc from cause`
set them in the same way as standard Python.
2022-11-13 19:52:50 -06:00

47 lines
851 B
Python

try:
Exception().__cause__
except AttributeError:
print("SKIP")
raise SystemExit
def print_exc_info(e):
print("exception", type(e), repr(e))
print("context", type(e.__context__), e.__suppress_context__)
print("cause", type(e.__cause__))
try:
try:
1/0
except Exception as inner:
raise RuntimeError() from inner
except Exception as e:
print_exc_info(e)
print()
try:
try:
1/0
except Exception as inner:
raise RuntimeError() from OSError()
except Exception as e:
print_exc_info(e)
print()
try:
try:
1/0
except Exception as inner:
raise RuntimeError()
except Exception as e:
print_exc_info(e)
print()
try:
try:
1/0
except Exception as inner:
raise RuntimeError() from None
except Exception as e:
print_exc_info(e)