My Links

Blog Stats

  • Posts - 15
  • Stories - 0
  • Comments - 11
  • Trackbacks - 759

Archives

Misc Blogs

.NET Tip: InnerExceptions hold the detail...

In researching .NET Remoting today I came across a forum conversation about a common app.config error.  In his response Michael Taylor writes:

...One of the changes that MS made in .NET 2.0 was to change the config file readers to support new features.  Inadvertently they also changed the normally useful error messages to generic, meaningless messages.  This was one of the those messages.  It was listed as a suggestion to revert to the original, more meaningful messages and MS fixed the general configuration errors that would cause such messages but it is possible they missed some.  I would verify that your config file is valid. 

Unlike many previous Microsoft code libraries (does anyone else start twitching when someone mentions Commerce Server 2000?), the .NET Framework's exception handler is pretty good about giving you the details of what went wrong.

For example, I once spent an hour debugging an XML problem that had a similarly cryptic error message which I no longer remember.  I finally thought to check the InnerException property of the exception and sure enough there was the detailed exception from the StreamReader telling me that I was an idiot.  I'd forgotten to reset the position on a MemoryStream before reading it.  It was late in the day ;-).

Since the root cause of my exception was different than the one mentioned above, I can't say how detailed the exception really was.  However, today I encounted the “Configuration system failed to initialize“ exception Michael mentions, and while the primary exception was generic, the inner exception told me exactly what was wrong (I'd cut and pasted a new app.config together and had left out part of the element hierarchy).

In addition to remembering to check the inner exception while debugging, it's a good idea to log them. Especially if you're writing code that has a lot of catch-all exception handlers.  A couple of short recursive functions make it easy enough:

private static string GetExceptionMessages(Exception e)
{
    System.IO.StringWriter sw = new StringWriter();
    System.CodeDom.Compiler.IndentedTextWriter tw = 
        new System.CodeDom.Compiler.IndentedTextWriter(sw);
    GetExceptionMessages(tw, e);
    return sw.ToString();
}
private static void GetExceptionMessages(System.CodeDom.Compiler.IndentedTextWriter tw, 
                        Exception e)
{
    if (e.InnerException != null)
    {
        tw.WriteLine(e.Message);
        tw.WriteLine();
        tw.Indent++;
        GetExceptionMessages(tw, e.InnerException);
    }
    else
    {
        tw.WriteLine(e.Message);
    }
}

Here I'm using the IndentedTextWriter from the System.CodeDom namespace, but you could always roll your own indented textwriter or just add a function to emit x # of tabs.  Whatever works for you.

Just for grins, I added the code above and recreated the error condition, here's the log output:

Configuration system failed to initialize
    Unrecognized configuration section application.
    (C:\Code\FeedManager\bin\Debug\FeedManagerClient.vshost.exe.config line 4)

posted on Sunday, May 28, 2006 5:10 PM

Feedback

No comments posted yet.
Title  
Name  
Url
The Magic Anti-Spam Word (MASW) is "hootie"
MASW
Comments   
604 Galer St. #324 - Seattle, Washington 98109 - 206.755.5565 - brian@conduction.com