The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
Global Exception Handling in .NET Core is used to catch all unhandled errors in one place instead of writing
try-catch in every controller or service.
In ASP.NET Core, we can handle exceptions globally using:
Middleware (Best way)
UseExceptionHandler()
Custom Exception Middleware
Filters (MVC)
This article explains best practice for production.
1. Why Global Exception Handling?
Without global handling:
try
{
}
catch
{
}
in every method → bad practice.
Problems:
Duplicate code
Hard to maintain
Hard to log errors
Not production safe
Global handling solves this.
Centralized
Clean code
Logging friendly
Production ready
2. Method 1 — UseExceptionHandler (Simple Way)
In Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseExceptionHandler("/Home/Error");
app.Run();
Controller:
public class HomeController : Controller
{
public IActionResult Error()
{
return View();
}
}
This catches all unhandled exceptions.
Good for UI apps.
3. Method 2 — Global Exception Middleware (Best Practice)
Best for API / large system.
Step 1 — Create Middleware
public class ExceptionMiddleware
{
private readonly RequestDelegate _next;
public ExceptionMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
await HandleException(context, ex);
}
}
private Task HandleException(HttpContext context, Exception ex)
{
context.Response.StatusCode = 500;
context.Response.ContentType = "application/json";
var result = new
{
Message = ex.Message
};
return context.Response.WriteAsJsonAsync(result);
}
}
Step 2 — Extension Method
public static class ExceptionMiddlewareExtension
{
public static IApplicationBuilder UseGlobalException(
this IApplicationBuilder app)
{
return app.UseMiddleware<ExceptionMiddleware>();
}
}
Step 3 — Register in Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseGlobalException();
app.Run();
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Global Exception Handling in .NET Core is used to catch all unhandled errors in one place instead of writing
try-catchin every controller or service.In ASP.NET Core, we can handle exceptions globally using:
This article explains best practice for production.
1. Why Global Exception Handling?
Without global handling:
in every method → bad practice.
Problems:
Global handling solves this.
2. Method 1 — UseExceptionHandler (Simple Way)
In
Program.csController:
This catches all unhandled exceptions.
Good for UI apps.
3. Method 2 — Global Exception Middleware (Best Practice)
Best for API / large system.
Step 1 — Create Middleware
Step 2 — Extension Method
Step 3 — Register in Program.cs
Now all exceptions handled globally.
4. Method 3 — With Logging (Recommended)
Production apps must log error.
Best practice.
5. Return Proper Status Code
Good API design.
6. Method 4 — Using Filter (MVC Only)
Register:
7. Best Practice (Interview Answer)
Use:
Best for large system.
8. Conclusion
Global exception handling in .NET Core should be done using:
This is production-level approach.