---
title: "How do I handle exceptions globally in ASP.NET Core?"  
description: "How do I handle exceptions globally in ASP.NET Core?"  
author: "Kacper Jankowski"  
published: 2026-11-13  
updated: 2026-11-13  
canonical: https://www.mindstick.com/forum/163008/how-do-i-handle-exceptions-globally-in-asp-net-core  
category: "C# and .NET"  
tags: ["asp.net core", "exception handling", "middleware", "Error Handling", "ProblemDetails"]  
reading_time: 1 minute  

---

# How do I handle exceptions globally in ASP.NET Core?

Our controllers have try/catch blocks everywhere that return a 500 with a custom message. Is there a cleaner way?

## Replies

### Reply by Moritz Wolf

Yes. Let exceptions bubble up and handle them in one place with middleware. That keeps controllers short and makes every error response look the same.

In .NET 8 the simplest way is IExceptionHandler:

```
public class GlobalExceptionHandler : IExceptionHandler
{
    public async ValueTask<bool> TryHandleAsync(HttpContext ctx, Exception ex, CancellationToken ct)
    {
        // log the exception here
        ctx.Response.StatusCode = 500;
        await ctx.Response.WriteAsJsonAsync(new ProblemDetails { Title = "Something went wrong" }, ct);
        return true;
    }
}
```

Register it with `AddExceptionHandler<GlobalExceptionHandler>()` and `AddProblemDetails()`, then call `app.UseExceptionHandler()`.

Log the full exception on the server, but never send stack traces to the client in production. Keep try/catch only where you can recover from a specific error.


---

Original Source: https://www.mindstick.com/forum/163008/how-do-i-handle-exceptions-globally-in-asp-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
