Module (93%)
Section (93%)

Advanced exceptions - the traceback attribute

Each exception object owns a __traceback__ attribute.

Python allows us to operate on the traceback details because each exception object (not only chained ones) owns a __traceback__ attribute.

Let's examine such an object while we’re preparing our rocket to launch.

Final check procedure The captain's name is John The pilot's name is Mary The mechanic's name is Mike <traceback object at 0x00753300> <class 'traceback'>

output


Code

class RocketNotReadyError(Exception):
pass


def personnel_check():
try:
print("\tThe captain's name is", crew[0])
print("\tThe pilot's name is", crew[1])
print("\tThe mechanic's name is", crew[2])
print("\tThe navigator's name is", crew[3])
except IndexError as e:
raise RocketNotReadyError('Crew is incomplete') from e


crew = ['John', 'Mary', 'Mike']

print('Final check procedure')

try:
personnel_check()
except RocketNotReadyError as f:
print(f.__traceback__)
print(type(f.__traceback__))
{{ dockerServerErrorMsg }} ×
{{ errorMsg }} ×
{{ successMsg }} ×