2

I'm pretty new to MVC ASP.NET. I read about OnException override method. I'm thinking, whether I should put try catch {throw} on Controller or Model in order for OnException to be invoked. OR, I try catch not required, OnException will be invoked autmatically if any exception occurs?

Thanks Heaps.

2 Answers 2

2

"Called when an unhandled exception occurs in the action."

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception.aspx

If you don't handle (i.e. "catch") an exception, the OnException method should be called.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. Spot on, so I should remove all my try catch where it just throws the exception to bubble up?
@flybyte: That depends on what you're doing with the exceptions you're trying to catch. But yes, the best strategy is usually either to not catch exceptions at all until the highest possible level (OnException), or to throw a new exception with additional contextual information, using the original exception as an InnerException.
2

I ended up doing this:

Created LogAndRedirectOnErrorAttribute class that uses abstract class FilterAttribute and implements IExceptionFilter as shown below:

public class LogAndRedirectOnErrorAttribute : FilterAttribute,IExceptionFilter 
    {
        public void OnException(ExceptionContext filterContext)
        {
            //Do logging here
            Util.LogError(Utility.GetExceptionDetails(filterContext.Exception), TraceEventType.Critical.ToString());

            //redirect to error handler
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(
                new { controller = "Error", action = "Index" }));

            // Stop any other exception handlers from running
            filterContext.ExceptionHandled = true;

            // CLear out anything already in the response
            filterContext.HttpContext.Response.Clear();
        }
    }

And on Each Controller Class where necessary, use the above attribute:

[LogAndRedirectOnError]
public class AccountController:Controller
{
.....
}

2 Comments

The filter implementation is very tidy. Have you considered creating a base class for your controllers to derive from that has the filter applied to it? This would reduce the usage of the filter attribute. You might also pass the logger dependency into the base class constructor, ideally as an interface and employ an IoC container to handle the construction logic.
You could simply add that attribute as a filter so it will be on all controllers by default: filters.Add(new LogAndRedirectOnErrorAttribute ()); It's also questionable to include redirecting in this handler since you could use the already built in customErrors functionality instead and let it be configurable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.