Inner Exception's Inner Exception madness

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
I could make the title sound even less safe for work.. :)

In my VB.NET 2005 code, in a Function A, I catch an exception, then wrap it in a friendlier one, and throw it up. The calling function catches it, wraps it in its own friendly description, and throws it up too. Eventually, this bubbles up to the main function, which now needs to show it to the user.

How can I display a chain of nested error messages to the user (or put in a log)? If the original exception from Function A was wrapped and thrown up 5 times, do I do...

MsgBox (Exception.InnerException.InnerException.InnerException.InnerException.InnerException.Message) ?

I also want to include the full stack trace, so do I need to do the same for ...InnerException.InnerException.StackTrace? Sounds insane ;)

Is there a single property or method which will show me the entire chain? ex.ToString works but seems shady.

Thanks!
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
The stack traces should be chained together, that's the entire point of passing the InnerException.

I don't think the messages are passed, but in general, why would you want your end user to see the innermost exception message? The end user generally cares about is their particular transaction succeeded or failed, and what their options are if a failure did occur. The logic to distinguish between those types of failures and what to present to the user should be designed into your app.
 

SoftwareEng

Senior member
Apr 24, 2005
553
4
81
I still need the entire chain of exceptions for logging and E-mailing to the developer, even if I only display the outer, friendly one to the user.

So how can I get that entire chain of messages and stack traces, like:

Account Details window could not be displayed -> Could not load Account from DB -> Accounts table not found -> SQLengineError blah blah

(if this were the chain from outermost exception to the inner most)
 

Tencntraze

Senior member
Aug 7, 2006
570
0
0
Why not just use some sort of loop, like:

StringBuilder sb = new StringBuilder(ex.Message);
Exception innerException = ex.InnerException;
while(innerException != null)
{
sb.AppendFormat("\n{0}", innerException.Message);
innerException = innerException.InnerException;
}

In theory this should work, but I just typed it off the top of my head. Hopefully you get the idea.
 

imported_Dhaval00

Senior member
Jul 23, 2004
573
0
0
Originally posted by: Tencntraze
Why not just use some sort of loop, like:

StringBuilder sb = new StringBuilder(ex.Message);
Exception innerException = ex.InnerException;
while(innerException != null)
{
sb.AppendFormat("\n{0}", innerException.Message);
innerException = innerException.InnerException;
}

In theory this should work, but I just typed it off the top of my head. Hopefully you get the idea.

No need to do the extra work. Calling Exception.ToString() will give you the entire StackTrace.
 

Tencntraze

Senior member
Aug 7, 2006
570
0
0
Originally posted by: Dhaval00
Originally posted by: Tencntraze
Why not just use some sort of loop, like:

StringBuilder sb = new StringBuilder(ex.Message);
Exception innerException = ex.InnerException;
while(innerException != null)
{
sb.AppendFormat("\n{0}", innerException.Message);
innerException = innerException.InnerException;
}

In theory this should work, but I just typed it off the top of my head. Hopefully you get the idea.

No need to do the extra work. Calling Exception.ToString() will give you the entire StackTrace.

I didn't know what kind of formatting he wanted to do, or what he wanted to do with the exception, so figured I'd just provide the basic idea.