In my code I have an exception thrown and caught using a typical try..catch. The message in the exception is An error occured while executing the query. Please check the stack trace for more detail.
There is also an InnerException with the message ValidationException was thrown.
There is no other InnerException. However, when I look at the exception through Visual Studio 2015, I can expand the Exception, go to the InnerException and expand that and I see:
InnerException: Nothing
InnerExceptions: Count=1
I can then expand the InnerExceptions branch and see what I assume is an AggregateException which, in this case, shows
(0): {"Error Parsing query"}
Raw View:
I can then expand the (0) group to see properties like "Detail" which gives a full and detailed error message as well as "ErrorCode" and many others.
When I try to reference the "InnerExceptions" through code via ex.InnerException.InnerExceptions, I can't because 'InnerExceptions' is not a member of 'Exception'.
How is it visible in the Visual Studio IDE but not available through code?
I am writing code that is using the IppDotNetSdkForQuickBooksApiV3 NuGet package. I mention this since I am not sure if this is somehow added on from Intuit's API or not. I've never encountered an InnerExceptions group before.
Just to be clear: Iterating through the InnerException property does not return the same error found in the "Detail" mentioned above.
The Exception
class doesn't have a member called InnerExceptions, however it is a base class of AggregateException which has. Visual Studio's debugger will figure out each objects' type and therefore is able to display every property they have.
However the Exception class's InnerException member is not of an AggregateException type, only a generic Exception. That said, Visual Studio has no way to figure out if your InnerException is going to actually be an AggregateException by type. To solve this, you need to cast.
I'm not really familiar with vb.net syntax, in C# land it would like this:
((AggregateException)ex.InnerException).InnerExceptions
or you can try safely casting like so:
if (ex.InnerException.GetType() == typeof(AggregateException))
{
var listOfInnerExceptions = ((AggregateException)ex.InnerException).InnerExceptions;
}
As far as I know there is a DirectCast(obj, type)
method in VB.net that could be used for this purpose, but I might be wrong about this.