Wednesday, September 15, 2010

Visual Studio Tips

I recently attended a class led by John Robbins on .NET Debugging which discussed how to use the various debugging tools in Visual Studio. There were a few ticks I found interesting that I thought I'd share.


Quickly Find a File and Open it
If you are like me and have solutions and projects that contain lots of files, it can sometimes be a trick to remember where that one particular file is. You can't remember the exact name, but know what it starts with. Pressing CTRL+D (the default hot key for Edit.GoToFindCombo) and then typing >open followed by the partial name of the file will bring down the list of files. You can then select the file of interest and press Enter to open it.


Placing a Breakpoint in a Function
Sometimes you know the name of a function that you want to place a breakpoint in, but don't have the file open. A quick way to put a breakpoint at the function is to press CTRL-D (the default hot key for Edit.GoToFindCombo), type the name of the function, and press  F9. This places a break point on the first line of the specified function. This is pretty useful if you are looking at a log file with a stack trace and want to jump straight to a particular function and debug.


Seeing the Current Exception
There are times when you catch an exception but don't care about the actual exception instance.


try
{
}
catch(InvalidOperationException)
{
    // Do something special, then throw
    throw;
}
When you're debugging, you might need the actual instance to dig deeper into the cause. If you put $exception into your watch window, you 'll see it.

2 comments:

  1. I believe once you've entered the catch block (once you've hit the opening brace) you can also mouse over the exception type (InvalidOperationException in this case) and click View Exception Details, or something like that. You need to click the ! that pops up first, though, which may be inconvenient.

    ReplyDelete
  2. Yeah, that works most of the time. There have been times where for whatever reason the '!' wouldn't come up.

    ReplyDelete