views:

126

answers:

1

I have a custom exception filter that I'm using to catch a custom exception that I wrote but for some reason when I throw my exception, it's not ever getting to the filter. Instead I just get an error that my exception was not handled by user code. Can anyone please provide some advice/assistance as to how I should have this set up? Relevant code is below:

// controller    
[CustomExceptionFilter]
    public class SomeController : Controller
    {    
        public SomeController()
        {

        }
        public ActionResult Index()
        {
            SomeClass.SomeStaticMethod();
            return View();
        }
    }

that's the controller with the customexception attribute

// some class (where exception is being thrown)
public class SomeClass
{
    public static void SomeStaticMethod()
    {
        throw new MyCustomException("Test");
    }
}

that's the class (for my test) that throws the exception (I've also tried throwing it directly on the controller).

// Custom exception filter (want this to catch all unhandled exceptions)
public class CustomExceptionFilter : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        if (filterContext.Exception.GetType() == typeof(MyCustomException))
        {
            // do stuff
        }
    }
}

that's the custom exception filter...it's never being reached when the code is executed and the exception is thrown. Instead I get the error mentioned above. Everything I've read indicates that this is the proper way to set this up, but when I put breakpoints in my custom filter, it's never being hit....

What am I missing here?

TIA

+1  A: 

Once you've handled your error, you need to let the filter context know it has been handled. Like this:

filterContext.ExceptionHandled = true;

This should be in your '// do stuff' section.

I've copied your code, and the filter is getting called fine. The only difference I made was I added the exceptionHandled code and adding my breakpoint at that line.

Richard
Hopefully you're still following this even though it's 4 days old. I just got back to this and added your line of code inside my if block....it's never being reached as my OnException method is never being called. Is there a flag somewhere that I need to set (maybe in web.config) or something to tell the application to handle exceptions in this filter? I"m still getting the error on the "throw new MyCustomException("Test");" line indicating that the exception was unhandled by user code.
Kyle
I didn't need to set any flags, or add anything to web.config. It just worked. When I step through in debug mode: First the exception is thrown and it breaks there. Then I press play and it goes through to my breakpoint in OnException. Have you tried stepping through in debug mode?
Richard
I assume that everything is in the same namespace and you only have one CustomExceptionFilter and CustomException.
Richard
Also, try commenting out the code that checks the exception type in case the filter is getting called but it's not matching your exception for some reason.
Richard
as it turned out, it was the debugger that was killing me. When I just rt click the default.aspx page and 'view in browwer' I got the desired result. I'm thinking my debug settings need some adjustment somehow? I'm not sure how to tone it down in Visual Studio. Thanks for your responses. I did need the filterContext.ExceptionHandled = true so I'll mark that as the answer :)
Kyle
Cool. Glad you've sorted your problem.
Richard