VS 2003 Tip #3: View exception information with $exception

Every now and then a feature gets put into the product that just gives you this tickle of excitement every time you think about it. $exception I find is one of those features.

Ok – so what scenario would I use it in? Lets say you’re writing some managed code. You’re debugging your merry way through the code when voilà, it crashes. Sigh. You see the little error dialog with additional information but to explore your code you have to dismiss it. As you go through your code, you wonder what that message said again? So you write a try catch and repeat the process.

Well, in Everett the debugger team put in a little feature called $exception. The next time you crash in C# or J# go ahead and dismiss the dialog. Open the locals window and take a peek at $exception there (chills are setting in right now). Expand it and you can see all the delightful things inside the Exception object – Messages, Stacktrace, Inner Exception etc – everything you would have gotten had you put a try catch around the code and caught the exception object. Is that cool or what?

Comments (18)

  1. Corrado Cavalli says:

    VB does not seems to have $Exception…

  2. Hans Jergen Ohff says:

    VB doesn’t seem to have very much but skipping right along….*quickly shuffles crowd onwards*…

  3. Dennis says:

    This is cool but why aren’t you catching the exception in the first place?

    Personally, I just wrap everything in a try, call a central exception handler, and while developing put a breakpoint in the handler…

  4. im says:

    Quick! Everyone copy Dennis – his is the only way. (apparently)

  5. Dennis says:

    Oh, gimme a break, im…didn’t say it was the only way, just another way, which I happen to use…my question wasn’t rhetorical.

  6. Eric says:

    "This is cool but why aren’t you catching the exception in the first place?"

    Because you want to allow the exception to bubble up the call stack? Often the immediate (or a not-so-immediate) caller of a method will have much better information about the context of an exception than the actual site where the exception was thrown. In those cases there’s no point to adding a try…catch block since the catch would just be rethrowing the exception without adding any real value. You might as well bubble the exception up and see if the next higher caller can add any information.

    Obviously at the top level you probably want to catch the exception whether you can add any value or not, so that you can log it and display a pretty message to the user (if there is a UI).

  7. Mike says:

    "This is cool but why aren’t you catching the exception in the first place?"

    You shouldn’t catch an exceptoin unless there’s something you can do to recover from it or you’re the last stop before the user get’s it and need to log it and replace with a user friendly message.

    "call a central exception handler"

    This statement kind of addresses that but if you’re not the last stop before the user gets it, you could be hijacking control and not allowing a component that called you to recover.

  8. Keith Hill says:

    I like this feature, having used it a good bit (after seeing it in the debug tip/tricks session at PDC). However, for "discoverability" why not add a "View Last Exception" to the Debug menu with an embedded property grid for viewing the properties of the exception?

  9. Hmmm – I often go to the Debug-Exceptions dialog and just turn on "break into the debugger" for all CLR exceptions. Yes, you occasionally get first chance exceptions that you can just let go by, but it works quite well generally speaking.

  10. I’ve flip-flopped back and forth between VB and C based languaged, and finally ended up in the lap of vb.net after writing ASP pages for quite some time in vbscript, and am just wondering why C# has so many more useful features than VB. We know that they all compile down to MSIL, but still, there are many more useful things in C# than in VB.Net and I can’t help but feel left out. I love VB, and don’t want to give it up. Just a thought.