An exception is an unexpected runtime event that interrupts normal execution. In an ASP.NET Core request pipeline, an unhandled exception propagates upward; if no middleware catches it, the web server returns HTTP 500. section 4.3
Handle exceptions and related failures according to the kind of response the application needs:
- Use error-handling middleware to catch failures and return either useful error details or a friendly, generic response. section 4.3
- Treat expected request problems differently from unexpected failures: return 404 Not Found or 400 Bad Request instead of throwing an exception. section 5.3
- Remember that a missing route can produce 404 without an exception, while an unhandled exception normally produces 500. section 4.3
Requesting a nonexistent resource
A minimal API request for /fruit/f1 throws an exception when no Fruit with ID f1 exists, producing a 500 response. Returning an IResult instead lets the endpoint deliberately return 404 Not Found or another suitable status. section 5.3
Figure 4.14 An exception in the endpoint middleware propagates through the pipeline. If the exception isn’t caught by middleware earlier in the pipeline, a 500 “Server error” status code is sent to the user’s browser.
Trace how an exception moves backward through endpoint and earlier middleware before reaching the browser as a 500 response. section 4.3
























