Let us assume that a particular Exception ‘SomeException‘ is part of the exception stack,
so let us assume ex.InnerException.InnerException.InnerException is of type ‘SomeException‘
Is there any built-in API in C# which will try to locate a given exception type in exception stack?
Example:
SomeException someExp = exp.LocateExceptionInStack(typeof(SomeException));
No, I don’t believe there’s any built in way of doing it. It’s not hard to write though:
If you’re using C# 3 you could make it an extension method (just make the parameter ‘this Exception outer’) and it would be even nicer to use:
(Note the shortening of the name as well – adjust to your own taste 🙂