8

I have an unhandled exception that causes the Exception Assistant dialog box to appear.

When I click on View Detail..., the exception itself has some values in it's custom object model that will not evaluate in the property grid, but I know I can evaluate it in the immediate window. (In this case the property grid won't let me drill down into a collection, but there can be other cases)

Without altering the code to add a try block, how can I go to the immediate window and evaluate expressions on the unhanded exception?

The answer will probably be some magic that I just don't know yet, like ?this.CurrentException or

something involving System.Diagnostics.StackFrame or who knows. Something clever.

There is a way to navigate to it using the debugger thread, but that's quite complicated. If you can take that and make it simple with a wrapper that might be a solution.

toddmo
  • 20,682
  • 14
  • 97
  • 107

3 Answers3

11

Did you try setting the debugger to break when the exception is thrown instead of just when it is User-unhandled?

To do this go to VS2010 main menu and select the 'Debug' menu Next select 'Exceptions...'

That will bring up a dialog like: Debug -> Exceptions.. menu

Select the Thrown column

Now when your exception is thrown and you should be able to evaluate your local variables in the Immediate window.

In the Locals tab I can see the $exception variable: Local variables contains $exception

I'm able to use the "$exception" variable in the immediate window: Immediate Window accessing $exception

Update: Also for easy toggling of Exception handling I recommend using the Exception Breaker Visual Studio Extension, which allows you to toggle break on exception handling on and off from the tool bar instead of having to drill into the Debug menu.

Chad Dienhart
  • 5,024
  • 3
  • 23
  • 30
  • I'm specifically trying to evaluate the unhandled exception itself. Visual Studio is breaking when I have the exception I'm interested in. Do you know what I would type in the immediate window to evaluate expressions involving the unhandled exception? – toddmo Nov 07 '13 at 15:44
  • 2
    I'm able to access it via "$exception" in VS2010. I've updated the post with images. – Chad Dienhart Nov 07 '13 at 18:53
  • I know you mentioned that you don't see "$exception" as a variable, but with the "break when an exception is: Thrown" checked it is there for me. I tried it and the variable is not there when the "Thrown" box is unchecked. – Chad Dienhart Nov 07 '13 at 19:02
  • Thank you for making that distinction! that works for me :-) I think that's as good of an answer as there is for this version. I hope in 2012 the checkbox won't have to be checked. – toddmo Nov 07 '13 at 20:49
  • I want to add, that you can uncheck Thrown in the Exceptions dialog if you turn off the Exception Assistant in Debugging Options. I don't know why they are mutually exclusive, but that's probably as good as it gets. – toddmo Nov 07 '13 at 20:58
1

I don't know about Visual Studio 2010 but in Visual Studio 2012 when an unhandled exception occurs it's shown in the Locals window with the name $exception.

  • 1
    We are using Visual Studio 2010, because we like the colored icons. (Just kidding), but we are stuck with that version. It does not appear in the Locals window. I tried the expression in the watch window and no luck. – toddmo Nov 06 '13 at 23:03
  • I don't see $exception int he locals windows in Visual Studio 2015 either. – Luis Perez Mar 31 '17 at 12:44
0

there is a property that will not evaluate in the property grid

If the property can't be evaluated, the debugger won't help you as you have seen; hence the immediate window would just do the same.

I had a invalid property such that it would throw an exception in VS2010 and cause the debugger to crash when I attempted to evaluate it. Nulls being returned from properties were not nice to the debugger.

I recommend that you go old school on the issue and put Trace.Write within the property and elsewhere and monitor the write reports in the output window instead.

How to trace and debug in Visual C#

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • I have edited my question to clarify. Sorry for the confusion. I'm not debugging a property or expression in my code. The expression I want to evaluate involves the unhandled exception and it's part of the exceptions object graph. Thanks. – toddmo Nov 07 '13 at 16:00