What is the recommended strategy for exceptions in a layered/enterprise app?
Short answer
Throw specific exceptions in lower layers, don't swallow them, translate to a consistent response at the boundary (middleware/ProblemDetails), and log with context. Avoid using exceptions for control flow.
Advertisement
AModel answer
Throw specific exceptions in lower layers, don't swallow them, translate to a consistent response at the boundary (middleware/ProblemDetails), and log with context. Avoid using exceptions for control flow.
app.UseExceptionHandler(); // central translation to ProblemDetailsReal-world example
One middleware converts domain and unexpected exceptions into consistent JSON errors for all API clients.