I work in a rather complex environment where sometimes the real exception is an inner exception several layers deep… so I was tempted to write code like this:
Dim innerEx = ex.InnerException
While innerEx IsNot Nothing
innerEx = innerEx.InnerException
End While
However, according to the MSDN documentation the ToString method calls ToString on its InnerException… since that is an exception, can I assume that this will work recursively?
It will work fully recursive unless the outermost exception decides to override ToString() without calling the base version. Then of course the method returns whatever the overridden version decides to return. Developers are therefore highly discouraged from overriding ToString in their own exceptions in order to keep the behavior of calling ToString() on an exception predictable.
By the way, it does not matter if any of the inner exceptions overrides ToString() because during the recursion the public (virtual) ToString() method is not used. Instead a private method ToString(bool) is invoked which does all the work and calls itself on the inner exception if necessary.