• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Inner Exception's Inner Exception madness

SoftwareEng

Senior member
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!
 
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.
 
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)
 
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.
 
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.
 
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.
 
Back
Top