Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle an exception occurring in a filter?

I have various filters before a URL hits an controller action.

Very few filters have a complex logic which might throw exceptions.

Now, how do I catch these exceptions ?

For eg. I have a filter which handles exception occuring in a controller method.

[ActionFilter1] is a filter which handles any exception occuring in a controller method.

public override void OnActionExecuting(ActionExecutingContext filterContext)
{

 //exception occuring here

}

One way is to do something like this:

public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
     try {
     //exception occuring here
     }
     catch {
     //log here
     }
    }

since there are n filters, I do not want to repeat this logic of adding try & catch to each and every filter.

In that case, how can I proceed with a single place error handling which can handle any exceptions occurring in all these filters ?

like image 384
now he who must not be named. Avatar asked Mar 12 '23 06:03

now he who must not be named.


1 Answers

Read this article. You will find the answer http://www.codeproject.com/Articles/850062/Exception-handling-in-ASP-NET-MVC-methods-explaine

like image 184
divya Avatar answered Mar 23 '23 05:03

divya