First-chance exceptions in System.Web.MVC

May 17, 2010 at 1:54 AM

While investigating the code base for MVC2 during runs of NerdDinner, I noticed that a number of exceptions seem to be raised and swallowed by the mvc 2 framework.  When breaking on ALL unhandled exceptions, I find that this happens when searching for a Partial View (such as NerdDinner's LoginStatus.ascx) in the /Shared folder of the site.

System.Web.Mvc.VirtualPathProviderViewEngine(...) seems to try looking in the primary folder first (in this case /Home), and then trying the /Shared folder after ignoring the HttpException "The file /Views/Home/LoginStatus.ascx does not exist". It then of course succeeds and all proceeds as expected.

I'm wondering if anyone knows why System.Web.Mvc was coded this way. I've been taught (wrongly?) that its not correct to write code to <expect> and ignore exceptions like this. Exceptions should be used for truly "exceptional" conditions that do not happen when things are running normally.  It bothers me a bit that the mvc framework itself does this. It seems it could have been coded to more properly check for existence of the file before attempting to load it, instead of blindly loading it and then ignoring the error that results. Is it some sort of performance consideration, where its faster to just trash the exception than to check file existence?

This is obviously not a big problem, I just don't like seeing a bunch of exceptions ignored.  Its hard enough making sure my own code is running properly without being distracted by the framework burping out messages like this. There are a number of these (13!) just to get the home page loaded for NerdDinner.  Over half of these are for HttpException as described above, but others also show up.

Again, not a big problem, but I'd appreciate it if anyone knows why MVC 2 was coded this way.

Thanks !