WPF Global Exception Handler


I decide to write this post because I’m facing too many applications that crash, yes just crash – no massage or recovery just crash!!!

For some application it feels like 50% chance to get an exception, for me when an application crashes frequently I stop using it when I can find a proper replacement, but not always I can find suitable a replacement.

In case you don’t know what I’m talking about here is a small application I’ve created to demonstrate what I’m talking about.


Now, it doesn’t required a lot of work to handle those unhandled exceptions, in WPF application you need to register DispatcherUnhandledException event, from that point you need to choose how to handle that exception but the important part is handling and tracking those exceptions.

Start by register to DispatcherUnhandledException event and make sure you mark the ExceptionArgs Handled property as true, this tells the application to prevent default unhandled exception processing – CRASH.

public MainWindow() { InitializeComponent(); Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException; } void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { e.Handled = true; UnhandledException exWin = new UnhandledException(e.Exception); exWin.ShowDialog(); }

I’ll prefer to send a error report to the application creator but you can implement your own solution that doesn’t crash….

In my demo I’ve create new Window that will display the exception and some details to the user, I’ve also added TranslateStack method to format the StackTrace log.

public UnhandledException(Exception ex) { InitializeComponent(); if (ex == null) return; txtMessage.Text = ex.Message; txtDetails.Text = TranslateStack(ex); } private string TranslateStack(Exception exception) { string format = "{0};{1};{2};{3};{4}"; StringBuilder builder = new StringBuilder(); StackTrace trace = new StackTrace(exception, true); var stackFrames = trace.GetFrames(); if (stackFrames == null) return builder.ToString(); foreach (StackFrame frame in stackFrames) { string name = frame.GetMethod().Name; StringBuilder subBuilder = new StringBuilder(); string fileName = frame.GetFileName(); string str = frame.GetFileLineNumber().ToString(CultureInfo.InvariantCulture); if ((frame.GetMethod() == null) || (frame.GetMethod().DeclaringType == null)) continue; string fullName = frame.GetMethod().DeclaringType.FullName; foreach (ParameterInfo info in frame.GetMethod().GetParameters()) { if (subBuilder.Length != 0) { subBuilder.Append(", "); } subBuilder.Append(info.ParameterType.Name + " " + info.Name); } if (builder.Length != 0) { builder.Append("\r\n"); } builder.AppendFormat(CultureInfo.CurrentCulture, format, new object[] { name, subBuilder, fullName, fileName, str }); } return builder.ToString(); }


Add comment
facebook linkedin twitter email

Leave a Reply

one comment

  1. Michael Loewenstein10/12/2014 ב 00:13

    Indeed it is a good idea to have a global exception handler. I think you should always handle the exception by closing the application, since you have no idea what might have gone wrong, and keeping the application alive might damage your data or mess up your BL. Just popup a costume message and close the app.