Propagation Past a Mismatched Except
A custom exception subclasses Exception, and one caller only guards against KeyError:
class InsufficientFundsError(Exception):
pass
def charge(amount):
if amount > 100:
raise InsufficientFundsError(f"cannot charge {amount}")
print(f"charged {amount}")
def process_order(amount):
try:
charge(amount)
except KeyError:
print("handled a KeyError")
finally:
print("cleanup")
process_order(50)
process_order(500)
print("done")
What is printed, and does print("done") ever run?
Sign in to answer questions and track your progress
Sign In