C# Interview Question #167
What is global exception handling in ASP.NET Core?
ASP.NET Core, DI & Middleware Senior Advanced
Quick Interview Answer
Global exception handling centralizes the conversion of unhandled exceptions into consistent HTTP responses and logs.
Detailed Explanation
Global exception handling centralizes the conversion of unhandled exceptions into consistent HTTP responses and logs.
Instead of placing repetitive try/catch blocks in every controller action, applications can use exception-handling middleware or ASP.NET Core's exception-handler infrastructure.
Expected business failures should often be represented explicitly and mapped to suitable HTTP status codes, while unexpected exceptions should be logged and normally return a generic server-error response without exposing internal stack traces to clients.
Code Example
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
// APIs can also use centralized exception handling
// to produce ProblemDetails responses.