How to evaluate unhandled exception properties in immediate window
Asked Answered
P

3

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.

Pressman answered 6/11, 2013 at 22:20 Comment(0)
M
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.

Mayer answered 7/11, 2013 at 1:13 Comment(5)
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?Pressman
I'm able to access it via "$exception" in VS2010. I've updated the post with images.Mayer
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.Mayer
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.Pressman
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.Pressman
A
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.

Anaesthetize answered 6/11, 2013 at 22:58 Comment(2)
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.Pressman
I don't see $exception int he locals windows in Visual Studio 2015 either.Mangrove
F
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#

Fluoroscopy answered 7/11, 2013 at 0:1 Comment(1)
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.Pressman

© 2022 - 2024 — McMap. All rights reserved.