When a new MVC3 project is created, [HandleError] attribute is by default registered as GlobalFilter in GLobal.asax. However, if I comment it and execute following (with custom error mode on), it still works. I do see ErrorView with ErrorInfo model populated. Then what is the need for registering HandleError in Global.asax?
[HandleError(ExceptionType = typeof(NullReferenceException),View = "ErrorView")]
public ActionResult Index()
{
throw new NullReferenceException();
return View();
}
That is setting the default MVC exception handling policy. It will render the
/Views/Shared/Error.cshtmlview when an unhandled exception arises, without you having to explicitly add the HandleError attribute on every controller or action.You can then add more specific
HandleErrorattributes to your controller and\or actions, so you could display another error view than the default view or handle a more expecific exception type.For the HandleError filters to work (global or not), you just need to make sure the custom errors are enabled in the web.config, as in
<customErrors mode="On" />(The default is RemoteOnly, so during development they won´t be executed)This is nicely explained (following an example) here