asp.net-mvchandleerror

Trouble with HandleError


I have the following Action Method:

    [HandleFtmsError]
    public ActionResult PerformanceChart(ChartViewModel chart)
    {
        var x = 1;
        var y = 0;
        var z = x/y;
        return Json("");
    }

where HaneleFtmsError is defined as:

public class HandleFtmsErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{
    public override void OnException(ExceptionContext context)
    {
        base.OnException(context);
        if (context.ExceptionHandled)
            RaiseErrorSignal(context.Exception);
    }

    private static void RaiseErrorSignal(Exception e)
    {
        var context = HttpContext.Current;
        ErrorSignal.FromContext(context).Raise(e, context);
    }
}

I thought that attribute over the action method would have been executed with a DivideByZero exception, but it's not working. All I'm seeing is the code breaks on the line where I'm doing the division. Am I doing something wrong?


Solution

  • When you say "the code breaks" do you mean it's breaking into the debugger? That's probably just the standard debugger behaviour, which you can change via the Debug menu's "Exceptions..." item. If you hit F5 again - or run without debugging - you may see the behaviour you expect.

    MVC isn't preventing the exception from being thrown (which is what the debugger's looking for) - it's just handling the exception by noticing the attribute on the controller and passing the information on appropriately. At the point where the debugger's breaking in, there hasn't been a chance for it to do that yet.