blah blah blah is here! blah blah » Close

up1down
link

Often I find that the most valuable error information is contained in the most inner exception. This should be simple but how can I iterate through the inner exceptions to get to the root of the problem?

last answered one year ago

1 answers

link

You can iterate through the inner exceptions as follows:

static void DoSomething()
{
try
{
// some code
}
catch(Exception ex)
{
ShowExceptionChain(ex);
}
}

static void ShowExceptionChain(Exception ex)
{
int num = 1;
do
{
Console.WriteLine("{0}. {1}", num, ex);
num++;
ex = ex.InnerException;
}
while (ex != null);
}
}

However, if you want to go directly to the innermost exception you can just call the GetBaseException method.

Feedback