ASP.NET Core Web API Interview Question #127

Why should controllers avoid repetitive try/catch blocks?

Error Handling, Logging, Caching, Rate Limiting, CORS & Reliability Senior Advanced

Quick Interview Answer

Repeated try/catch logic makes controllers noisy and often produces inconsistent logging and response formats. Expected business outcomes should normally be represented explicitly, while unexpected exceptions can be handled centrally.

Detailed Explanation

Repeated try/catch logic makes controllers noisy and often produces inconsistent logging and response formats. Expected business outcomes should normally be represented explicitly, while unexpected exceptions can be handled centrally.

Local exception handling is still appropriate when the action or service can genuinely recover, add meaningful context, or translate a specific exception into a known application outcome.

Code Example

[HttpGet("{id:int}")]
public async Task<ActionResult<ProductResponse>> Get(
    int id, CancellationToken token)
{
    var item = await _service.GetAsync(id, token);
    return item == null ? NotFound() : Ok(item);
}