Search code examples
c#.netvisual-studiodebuggingvisual-studio-debugging

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?


Solution

  • 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