1

I have a program that throws a Graph ODataError exception. My catch simply catches the exception as a generic exception:

catch (Exception ex) {
    Log.Write(ex);
}

When I put a breakpoint on the catch, I can hover over the ex object and drill down into its properties:

enter image description here

But if I type the following in the immediate window, I get this:

ex.Error

enter image description here

So, of course, any attempt to ex.Error.Message is also "undefined".

How can I get a reference to that inner ex.Error.Message?

3
  • 10
    Catch the right exception type. Jul 13 at 16:06
  • 3
    What would you expect to happen if you could use ex.Error, if a different exception type ended up being thrown?
    – Jon Skeet
    Jul 13 at 16:07
  • 2
    Use catch (ODataError ex) { ... } as first catch because the Error property is specific to ODataError and not available in Exception. You can add a second catch catching any non ODataError with catch (Exception ex). Jul 13 at 16:15

2 Answers 2

6

The IntelliSense popup (also known as "DataTip") allows you to analyze the object using the actual underlying type that the inspected object has. It will then use reflection to display and expose everything that object has to offer, regardless of the type of the inspected variable.

The Immediate Window allows you to execute bits of code as if you had written them in your own code. This code however must respect the declared type of the variables in the current scope.

You have several options here:

If you want to use the properties in your actual code, it's best to actually catch the specific exception type you want to handle. If applicable, you may also use a common superclass. For example, you can use catch (OperationCanceledException ex) to catch any OperationCanceledException, as well as the derived TaskCanceledException. In that case, ex will give you access to everything from OperationCanceledException and its superclasses, but not from things specific to derived classes. This is actually what you did there, but by chosing the most broad superclass System.Exception, there isn't really much specific insight ex can offer, apart from all things that the Exception class already introduced at the top level of the type hierarchy.

If you only want to inspect it temporarily within the immediate window, you can add a simple cast to the code line in your immediate window: ((ODataError) ex).Error.Message

Or, depending on your usings (which are also inherited from the local context), you may need to fully reference it, like so: ((Microsoft.Graph.Models.ODataErrors.ODataError) ex).Error.Message

4
  • The casting you mention is quick and easy but it would much more extensible with multiple catch blocks. One for ODataError, any number of other catch blocks for other specific exceptions, and lastly the general Exception catch block. This also allows for finetuned reactions to that exception. What if something freak happens and the ex can't be cast to ODataError? Now you got one more headache that could've been stopped upfront
    – Narish
    Jul 13 at 16:52
  • 2
    @Narish: This answer is not recommending using casting in the code itself. It specifically calls out the benefits of using multiple catch blocks like you mention. But when you're debugging, looking at the Immediate or Watch or a similar window, and you already know what type of exception you're looking at, casting just makes it easy to drill into the property for that known exception on-demand. Jul 13 at 21:46
  • That did end up fixing the problem (catching the right exception), but how can you anticipate every possible exception that might get thrown?
    – KWallace
    Jul 14 at 21:52
  • @KWallace The most common exceptions are those that you run into every time, where your code is simply wrong (NullReference, ArgumentNull, ArrayIndexOutOfBounds, InvalidOperation). These you fix whenever you come across them (unittesting helps a lot). Then there's those special to the code you're calling. These are usually documented in the method calls, and docs will help you evaluate whether your code is at risk of experiencing them. And finally: only catch the exceptions you can actually handle. If you pass on an argument and get an ArgumentException, just let it bubble up to the caller.
    – LWChris
    Jul 17 at 11:22
0

If you only want to access a property from a reference to a less-specific type, you could also use the is operator:

var msg = ex is Microsoft.Graph.Models.ODataErrors.ODataError odataErr?
    odataErr.Error?.Message : 
    ex.Message;

This should give you the ODataError.Error.Message if possible or the Execption.Message otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.