What Are Exception Filters in MVC?
MVC's exception filters make it possible to handle all controller method exceptions in one place. Creating a class that derives from the IExceptionFilter and FilterAttribute interfaces will do this.
How to Create an Exception Filter
The built-in IExceptionFilter interface is required for custom exception filters. The interface appears as above.
public interface IExceptionFilter { void OnException(ExceptionContext filterContext) }
Properties of ExceptionContext
• Result: The result returned by the invoked action. • Exception: Unhandled exceptions that were generated by the actions of the application. • ExceptionHandled: This property returns a Boolean value indicating whether or not any of the application’s filters handle the exception.
Advantage of Exception Filter
There is no stack unwinding with exception filters. In order to debug the program, if an exception is thrown in the try block, you can use exception filters to inspect the current status of the values there.