Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Exception.Data in Watch Window while debugging

How can you easily watch the contents of the Data property of an Exception in the Watch Window while debugging in Visual Studio? It is of the weird type System.Collections.ListDictionaryInternal.

I figured out you can see the Keys and Values separately:

        try {
            ... do something that throws exception with Data
        }
        catch (Exception ex) {
            throw;
        }
        finally {
        }

In the Watch window:

ex.Data.Keys.Cast<string>()
ex.Data.Values.Cast<string>()

But can you view it as a dictionary or something?

like image 891
Jeroen K Avatar asked Nov 24 '11 09:11

Jeroen K


2 Answers

System.Collections.ListDictionaryInternal is an IDictionary, so you could just evaluate the following expression in the Watch or QuickWatch window:

new System.Collections.Hashtable(ex.Data)

Edit: I co-created a commercial extension for Visual Studio called OzCode that makes this a lot easier. With it, you can simply hover over the Exception variable, right click it, choose Create Custom Expression, and type in new System.Collections.Hashtable([obj].Data) // Data. From that moment on, whenever you view an Exception, you'll be able to see its Data dictionary in a nicely formatted way without any manual steps, like so: screenshot

like image 142
Omer Raviv Avatar answered Sep 22 '22 21:09

Omer Raviv


I think your best bet is to create a function to output the Exception content, including the Data elements, if any, to the Output window using System.Diagnostics.Debug.Write().

like image 38
competent_tech Avatar answered Sep 23 '22 21:09

competent_tech