Exercise 3: A Second, Distinct Exception Type — Possible Solution ==================================================================== THE EXCEPTION CLASS AND EXTENDED MIDDLEWARE ------------------------------ class NotFoundError(Exception): pass def two_error_middleware(request, next_): try: return next_(request) except NotFoundError as e: return Response(f"404 Not Found: {e}", status=404) except ValueError as e: return Response(f"500 Internal Server Error: {e}", status=500) def crashes_with_value_error(request): raise ValueError("bad input") def crashes_with_not_found(request): raise NotFoundError("no such widget") app = App() app.use(two_error_middleware) app.add_route('GET', '/value-error', crashes_with_value_error) app.add_route('GET', '/not-found', crashes_with_not_found) VERIFIED, REAL RESULTS (both against a real live server) ------------------------------ GET /value-error -> 500, b'500 Internal Server Error: bad input' GET /not-found -> 404, b'404 Not Found: no such widget' WHY THIS WORKS AS AN ANSWER ------------------------------ The except clauses are ordered NotFoundError before ValueError - since NotFoundError doesn't inherit from ValueError here, order between these two specific clauses doesn't actually matter for correctness, but it does reflect the real, general Python rule that a more specific exception type should be listed before a more general one whenever a real inheritance relationship exists between the two. Both routes are verified against the same real running server rather than reasoning about the try/except abstractly, confirming each exception type produces its own distinct, correct status code and message rather than one generic error path swallowing both.