views:

18

answers:

1

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?

A: 

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.

Jon Skeet
Hi Jon, what I meant was the code's breaking into the debugger. When I hit F5 I expected then to stop at a breakpoint that I had set in the `RaiseErrorSignal` method. But that's not happening, so I thought that indicated that it wasn't working.
DaveDev
@DaveDev: No, try just hitting F5 to let it keep going, or change the divide by zero exception to *not* break into the debugger, so that MVC can handle it without the debugger getting in the way.
Jon Skeet