August
6th 2007
Displaying Graceful Exception Info

Posted under .net & Code & Logging & Software Design

For better or worse, exceptions are a part of life. Any non-trivial application is going to generate them, at least occasionally, when something exceptional happens. One shouldn’t rely on exceptions for control of flow; return values are much better at this in many ways. And a person should run tests to prevent exceptions, when it’s possible … make sure a number isn’t zero before dividing by it.

When an exception, which is really just a modern term for an error, does rear its ugly head, in addition to handling it gracefully, most applications need to do something with it. This might involve showing a message to the user, logging the fact that a problem happened for later inspection, or any number of things. A new open-source .net expands your options.

ExpressionFormatter is a static class, written in C# and targeting the Microsoft.net framework v2, that turns a managed Exception object into an html stream. In an ASP.NET application this lets you send a detailed, easily readable message directrly to the client (after they’ve been authorized!), but in a middle-tier, server-based application html is a fantastic way to store exception details for later review and drill-down analysis. In a Windows Forms client, you can log the exception for later use, and when it makes sense to, you can display it to the client using either a WebBrowser control or by writing an html file and using Process.Start to launch it.

Code Sample

Following is an example of a WinForms application making use of the ExpressionFormatter class:

namespace WindowsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}


protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
Thread t = new Thread(new ThreadStart(ForceException));
t.Start();

Thread.Sleep(100);
Close();
}

void ForceException() {
try {
Text = Cross-thread violation;

} catch (Exception ex) {
string filename = @”C:\test.html”;
ExceptionFormatter.OutputStream = new FileStream(filename, FileMode.Create);
ExceptionFormatter.RenderException(ex);
Process.Start(filename);
}
}
}
}

This is a simple test project that does almost nothing. A WinForms window is defined, and when it’s loaded (or shown), a new thread is created and assigned work it’s not allowed to do. In this case, because the call is being marshaled to a second thread, where an exception is thrown, it must be handled on this worker thread. WinForms was chosen as a test platform for ease of deployment, and because it gives an interestingly detailed message to deal with.

Output

At this point you may be wondering how all this work is justified; not simply the three lines of code that need to be placed within the catch block, but why not just use the Exception object directly?

There are a lot of properties attached to an exception; it’s message, source, a stack trace showing the code path that provoked the exception, and an optional IDictionary allowing extra information to be attached in a custom way. Add to all of this a recursive InnerException property, and suddenly the process of dealing with an exception can be more involved than it ought to be.

ExceptionFormatter renders all of these properties to an html document, with javascript for usability. You can see only the high-level details about an exception without being bothered by the entire stack trace, and with the click of a button you can expand this detail.

The rendered stack trace for an exception

More Info

ExceptionFormatter is open source freeware, and can be downloaded at http://exo-brain.com/Exception-Formatter/

Trackback URI | Comments RSS

Leave a Reply