Exception Handling & Custom Error Handling in .NET — Every Way, Every App Type
Rules
Validate early · catch specific · throw; · wrap with inner · using for cleanup · no empty catch · no exceptions for flow · global handler at boundaries · log once with correlation id · friendly messages, no internals.
ASP.NET Core
AddProblemDetails + AddExceptionHandler<T> + UseExceptionHandler() · UseStatusCodePages(WithReExecute) · exception filters for MVC · [ApiController] 400s · developer page only in Development.
.NET Framework
Web Forms: try/catch → Page_Error → Application_Error → customErrors/httpErrors · MVC 5: OnException → [HandleError]/IExceptionFilter → Application_Error · Web API 2: IExceptionHandler/IExceptionLogger.
Status mapping
Validation 400 · auth 401/403 · not found 404 · conflict/duplicate/concurrency 409 · business rule 422 · rate limit 429 · bug/unknown 500 · dependency down 502/503 · timeout 504.
Async & process
await rethrows first exception · WhenAll → check InnerExceptions · avoid async void · BackgroundService: try/catch per iteration · AppDomain.UnhandledException / TaskScheduler.UnobservedTaskException for last-chance logging.