Challenge 3: Custom Exception for Stock Levels — Possible Solution ==================================================================== class OutOfStockError(Exception): pass def purchase(stock, quantity): if quantity > stock: raise OutOfStockError(f"Cannot buy {quantity}, only {stock} in stock") return stock - quantity try: remaining = purchase(10, 3) print(f"Purchase succeeded, {remaining} remaining") except OutOfStockError as e: print(f"Error: {e}") try: purchase(10, 15) except OutOfStockError as e: print(f"Error: {e}") Output: Purchase succeeded, 7 remaining Error: Cannot buy 15, only 10 in stock WHY THIS WORKS AS AN ANSWER ------------------------------ class OutOfStockError(Exception): pass reuses the chapter's own InsufficientFundsError example precisely — a custom exception is just a class inheriting from Exception, with pass as the entire body being enough to make it a real, distinct, catchable exception type. purchase(stock, quantity) mirrors the chapter's withdraw() function structure: raise OutOfStockError(f"...") fires only when the requested quantity exceeds available stock, carrying a message built from an f-string that includes both numbers involved — the same "specific, descriptive message" approach the chapter's own custom exception example demonstrated. The first purchase(10, 3) call succeeds normally (3 does not exceed 10), so purchase returns stock - quantity = 7, and the try block's print statement runs. The second purchase(10, 15) call does exceed available stock, so it raises OutOfStockError, which is caught specifically by except OutOfStockError as e: — catching this custom type by name, rather than a generic Exception, makes it immediately clear from the except clause itself what failure is being handled.