Skip to content

Instantly share code, notes, and snippets.

@0xced
Created May 1, 2019 07:46
Show Gist options
  • Save 0xced/84821a5243bd0f6425daec84d44e75b5 to your computer and use it in GitHub Desktop.
Save 0xced/84821a5243bd0f6425daec84d44e75b5 to your computer and use it in GitHub Desktop.
Display exceptions with a TaskDialog or a MessageBox
// For setting up unhandled exceptions handlers: https://stackoverflow.com/questions/5762526/how-can-i-make-something-that-catches-all-unhandled-exceptions-in-a-winforms-a
// Using Ookii.Dialogs: https://www.nuget.org/packages?q=Ookii.Dialogs
static void DisplayException(Exception exception)
{
var baseException = exception.GetBaseException();
var title = @"Error: " + baseException.GetType().Name;
var message = baseException.Message;
if (TaskDialog.OSSupportsTaskDialogs)
{
using (var dialog = new TaskDialog())
{
dialog.MainIcon = TaskDialogIcon.Error;
dialog.WindowTitle = title;
dialog.Content = message;
dialog.ExpandedControlText = @"Hide details";
dialog.CollapsedControlText = @"Show details";
dialog.ExpandedInformation = exception.StackTrace;
dialog.Buttons.Add(new TaskDialogButton(ButtonType.Ok));
dialog.Show();
}
}
else
{
MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment