ASP.NET Core Interview Question #159
How can you implement centralized exception handling?
Logging, Error Handling & Background Services Mid-Level Intermediate
Quick Interview Answer
Centralized exception handling keeps repetitive try/catch blocks out of controllers and provides consistent responses for unhandled failures.
Detailed Explanation
Centralized exception handling keeps repetitive try/catch blocks out of controllers and provides consistent responses for unhandled failures.
ASP.NET Core supports exception handling middleware, and modern applications can also use IExceptionHandler implementations. A central handler can map known exceptions to suitable HTTP status codes and Problem Details responses while logging unexpected failures.
Local try/catch blocks are still appropriate when code can recover, retry safely, perform compensation, or translate an exception meaningfully.
Code Example
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails();
var app = builder.Build();
app.UseExceptionHandler();