Click here to Skip to main content
Click here to Skip to main content

WPF ExceptionViewer

By , 2 Oct 2012
Rate this:
Please Sign up or sign in to vote.

 

Introduction

Exceptions are inevitable, so they might as well look good. Just calling MessageBox.Show(ex.ToString()) doesn't cut it for me. I prefer something that not only looks better than that, but also makes it easy for users to report all the information about an exception.

This article presents the ExceptionViewer class. Similar to Microsoft's ExceptionMessageBox, it's designed for displaying an exception, its nested inner exceptions, and all their properties (using reflection). The source code download is a Visual Studio 2008 solution.

Background

For years, I used the ExceptionMessageBox class contained in Microsoft.ExceptionMessageBox.dll to display my exceptions. However, I was never comfortable redistributing that DLL with my applications. Also, users never seemed to realize they could access more details about the exceptions and copy those details into emails and bug reports. Finally, I'm using WPF now and ExceptionMessageBox just doesn't look like it's part of a WPF app.

The ExceptionViewer class in this article address those issues. It's simply a WPF window class you can add directly to your application (not requiring a separate DLL). The "Copy All To Clipboard" button is highly visible so users are more likely to use it.

Using the Code

To display an exception, just construct an instance and call ShowDialog().

try
{
    // Throw an exception
}
catch (Exception ex)
{
    ExceptionViewer ev = new ExceptionViewer("An unexpected error occurred in the application.", ex, this);
    ev.ShowDialog();
}

The last parameter to the constructor is optional. It specifies the Owner window. If specified, ExceptionViewer will set its own Style property to the Owner.Style and set WindowStartupLocation to WindowStartupLocation.CenterOwner. Of course, you can override these properties before calling ShowDialog().

The default title of the window is "Error - <ProductName>", where <ProductName> is taken from the AssemblyProduct attribute of the application (specified in ApplicationInfo.cs). You can change this by setting the static property DefaultTitle, or setting the instance property Title before calling ShowDialog().

Another static property, DefaultPaneBrush, specifies the Brush used for backgrounds of the two panes. It is null by default.

User Experience

The first screen shot (at the top of the article) is how the window first appears to the user. There's a splitter between the two panes, positioned so all of the left pane's content is visible without scrolling. The "All Messages" node is selected, which displays whatever header message is passed to the constructor in bold, followed by the "outer" exception message, it's InnerException message, and so on.

The following screen shot is how it looks when one of the exceptions is selected and "Wrap text" is unchecked. All of the exception's properties are displayed in the right pane, using the exception's type as the header. Note that the key/value pairs of the Data property, if any, are enumerated.

 

You can also expand an exception in the tree and view each property individually.

 

The "Copy All To Clipboard" button copies all of the information including the "header" message to the clipboard. Both Rich Text and Plain Text formats are placed on the clipboard so the "best" format is used when pasting.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

MarkLTX
Software Developer (Senior)
United States United States
Mark Lauritsen has been a software developer for as long as he can remember.

Comments and Discussions

 
GeneralEnjoyed the article. PinmemberJohn Holzer20-Dec-12 6:02 
GeneralRe: Enjoyed the article. PinmemberMarkLTX21-Dec-12 8:12 
GeneralMy vote of 5 Pinmemberchristoph braendle2-Oct-12 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 2 Oct 2012
Article Copyright 2012 by MarkLTX
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid