wormwood wormwood - 2 months ago 8x
C# Question

Exception of type System.Exception is not handled

Preface: Please don't refer me to Elmah. I'm trying to understand the MVC exception system for myself, not just use someone else's system for it.

I'm playing with MVC's exception system for my own enrichment, and I've run into an odd exception. I've written an OnException method as follows:

protected override void OnException(ExceptionContext filterContext)
if (filterContext.ExceptionHandled == true)
// log error

// mark error as handled to prevent rethrow
filterContext.ExceptionHandled = true;

//show custom error page
ViewBag.Code = filterContext.Exception is HttpException ? ((HttpException) filterContext.Exception).GetHttpCode() : 500;
ViewBag.Exception = filterContext.Exception;
filterContext.Result = View("Error");

I throw it via a simple enough call in a toy controller:

if(something strange and bad happens)
throw new Exception("When in trouble or in doubt, run in circles, scream and shout.");

I tested the exception and got the following exception:

An exception of type System.Exception occurred and was not handled in user code.

The string I sent into the constructor enters the exception system as part of the error message. When I continue execution through the error, I get to the error page with that message, but I keep getting the previous error when I try to run the code. The inner exception detail is null and the rest of the exception detail is too sparse for me to be able to use it to figure out what's gone wrong, or I'd read that instead of ask here.

I suspect I'm getting this exception because I'm not letting the base class finish handling the original exception, but I'm afraid I'm not entirely sure why. I've read a variety of articles on exception handling, but I'm not running into anything (that I know of) that has explained to me whether one needs to let the base class finish handling the exception or whether the handling I've done is sufficient. I've seen custom handling that did either or neither.

Or, conversely, if my suspicions are incorrect and it's something else entirely, does anyone know what it might be? The situation in which this is being thrown is very simple, and it's unlikely that anything else could be affecting this.

Does anyone know why I could be getting this error?


The code that we can see looks fine, so I'm left thinking that the exception comes from your ErrorLog method that we can't see.

You might be getting errors because a file is still in use (are you disposing the file streams correctly?) or for a variety of other reasons.

Your OnException method overrides MVC's error handling mechanism so when an exception happens during exception handling all hell breaks loose. The MVC framework probably has a very high level try { ... } catch { ... } somewhere just to save the application from crashing but it can't give you much more information on what happened.

Make sure your visual studio debugger breaks when an exception is thrown (https://msdn.microsoft.com/en-us/library/x85tt0dd.aspx) and then execute your code. Take a look at the exception thrown and the code involved. You'll either find why that specific part breaks or you'll find the subject of your next StackOverflow question. ;)