Exceptions are runtime errors that occur in an application. If they are not handled properly, the running program is terminated. How can we prevent that from happening? We can ensure that all unhandled exceptions are caught by using a global exception handler.
To implement global exception handling in ASP.Net Core Web API, we can take advantage of the built-in middleware UseExceptionHandler. A middleware is a software component inserted into the request processing pipeline that handles requests and responses.
When building an ASP.Net Core application, we can leverage various middleware components to customize the handling of requests and responses, and even inspect, route, or modify the request and response messages that flow through the pipeline.
In this post I will show how we can use the UseExceptionHandler middleware to handle exceptions globally in ASP.Net Core Web API.