liveBook · Manning

Exception

As explained in ASP.NET Core in Action, 3e

An exception is a runtime event representing an unexpected circumstance that interrupts normal program execution.

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.
CH04_F14_Lock3

Trace how an exception moves backward through endpoint and earlier middleware before reaching the browser as a 500 response. section 4.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains runtime causes, middleware propagation, and 500 responses, then contrasts exceptions with deliberate 404 and 400 results.
Examples
Uses NullReferenceException, an unhandled endpoint failure, a missing fruit ID, and a nonexistent route as concrete cases.
What it assumes
Assumes familiarity with requests and middleware pipelines while explaining exception behavior from basic runtime circumstances.
What you can do after
Diagnose failed requests by tracing exception propagation and distinguish unexpected 500 failures from intentional 404 or 400 responses.
How it connects
Connects exceptions with middleware pipelines, error handling, minimal APIs, HTTP status codes, and IResult responses.

Also explained in

Compare how 25 books explain exception →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.