InvalidOperationException: “Exception” in disguise? Or just the new ApplicationException?

Bookmark and Share

Microsoft added ApplicationException with the idea that all exceptions that you made should derive it. Fair point, your should be able to handle your own exceptions, right? The problem is what nobody used it and therefore Microsoft changed it’s recommendation to NOT use it. It’s just lying there in the framework like an old aunt that just don’t want to go away (I still love you all my dear aunts).

InvalidOperationException is also an exception that should be blown to hell. The description of the exception says this:

The exception that is thrown when a method call is invalid for the object’s current state.

What does that mean in the real world? When do exceptions get thrown in general? Well. When something unexpected have happened, as in when a method is called when it isn’t supposed to be. How else can an exception be raised? When not calling a method?

The InvalidOperationException is really like an Exception in a disguise. Try to search in a couple of open source projects after it (or your own). It’s thrown a lot. It’s a poor mans choice to exception handling. Ohhh, I’ve been guilty one too many times too. It’s so handy since it can be applied to all exceptional cases.

The reason to why I wrote this post is that I was debugging an ASP.Net MVC application and found that MS throws it when an action cannot be found in a controller. The result is that a Internal Server Error page is shown instead of a File Not Found page. Sure, it’s an error in the application and the proper controller have been found. But do the browser care about which framework is used in the web server? No. File not found is therefore the proper status code.

Microsoft should have created an ActionNotFound exception instead and derived it from HttpException (and set the 404 status code in the constructor initialization).

Edit:
I’ve filed a bug report for ASP.Net MVC: http://aspnet.codeplex.com/workitem/7588

This entry was posted in Architecture, CodeProject and tagged . Bookmark the permalink.
  • http://www.cuttingedge.it/blogs/steven Steven

    I agree that an unfound path in MVC should result in a File Not Found. However, from a framework’s perspective there is a very good reason for InvalidOperationException. It’s too bad that the MSDN documentation is a bit cryptic about this (but the Framework Design Guidelines actually aren’t). An important part of the description is the word ‘state’. For instance, an InvalidOperationException should be called by the Open() method of a connection, when the connection is already open. That has to do with the state of the object.

    InvalidOperationException is just like ArgumentException: It describes a programming error and you should not catch it. Throwing it in situation were you expect anyone to catch it is plain wrong. Many open source frameworks indeed throw InvalidOperationExceptions all over the place, which isn’t always correct, but not a problem unless you are expected to catch it. And still, from the perspective of the .NET framework it is still useful, simply because you’re expected to never catch it. ApplicationException however is so general that sometimes you are expected to catch it (or descendants) and sometimes you don’t. And everybody inherits from ApplicationException. That’s what makes it useless.

  • http://www.gauffin.com jgauffin

    Nice comment, thanks =) I do agree with you on all points. The problem is that InvalidOperationException has a cryptic description in MSDN and that can easily be misinterpreted resulting in overuse. Microsoft should really change it and also add an example showing a valid usage of it. As it is now, it IS used in the wrong places (and also in the correct places) which makes it so hard to handle the places where it is correctly used.

    Method ‘B’ (throws IOE incorrectly) calls method ‘A’ (throws IOE correctly).

    That condition makes it impossible to create proper exception handling since you might catch IOE from method ‘A’ which you really can’t handle. This is the problem with IOE in MVC.

    InvalidOperationException guidelines in The Framwork Design Guidelines can be found here: http://msdn.microsoft.com/en-us/library/ms229007.aspx (for all of you not reading this blog yet but will in the future (ohhh I want more than one reader even if you are a really cool reader Steven)).

  • http://www.gauffin.com jgauffin

    You can read more about ApplicationException here: http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=56

  • http://www.cuttingedge.it/blogs/steven Steven

    “even if you are a really cool reader”
    :-)