views:

80

answers:

2

It is a compiler warning and generally frowned upon to have unused variables in your code. Therefore the following will generate a warning because e is unused

try {
  throw new InvalidOpreationException();
}
catch(Exception e) {
}

The correct way of doing this is to not give the exception a name unless you're going to make use of it:

try {
  throw new InvalidOpreationException();
}
catch(Exception) {
}

But this "correct" way of doing things can be a hassle when debugging. You can set a breakpoint inside the catch but you won't have any idea why you got there unless you stop the app, name the exception, recompile and re-create the error. That's quite annoying to say the least.

The kicker is that that exception still exists, it just doesn't have an explicit name in this scope. Is there any way to access it anyway?

+16  A: 

Try adding $exception into the watch window. This will bind to the exception active on the current thread. You can analyze this variable to find out the runtime type and all of the details needed.

I've verified this works with Visual Studio 2008 and beyond.

EDIT

This type of feature is known as a pseudovariable in the visual studio debugger. You can get a complete list (broken down by language) at the following location.

JaredPar
Awesome thanks, thats exactly what I was looking for Jared
George Mauer
this is amazing
Chris McCall
Is there a list of any other tokens that are available? Can't really google $exception.
George Mauer
@George, I added a link with the appropriate data.
JaredPar
A: 

the compiler warning is there because you are declaring a variable witch is not used. If you need, do something with it and the warming will go away (log the error into a txt file for example)

Sergio
Sergio, that's what I said and of course all actual errors should get logged. Sometimes an exception needs to be caught because of a known condition and you simply need to recover, not actually do anything with the exception since it is the normal flow of the application. In that case you should not have it sitting there.
George Mauer