Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Windows Form with Exception information on catch?

I'm trying to find a way to create a nice windows form that will pop up when I encounter an unhandled exception. I currently have:

// Add the event handler for handling UI thread exceptions
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException );

// Add the event handler for handling non-UI thread exceptions
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( CurrentDomain_UnhandledException );

static void CurrentDomain_UnhandledException( object sender, UnhandledExceptionEventArgs e )
{
    MessageBox.Show( e.ToString( ), "Unhandled Non-UI Thread Exception" );
}

static void Application_ThreadException( object sender, ThreadExceptionEventArgs e )
{
    MessageBox.Show( e.ToString( ), "Unhandled UI Thread Exception" );
}

But what I'm looking for is to, in the threadexception methods, pop up a windows form with information about the error, and a continue/quit/restart whatever. This sounds very similar to something that, through gooogling, looks like its built in for certain cases, but is it possible to create some sort of modifiable / custom one that I can call?

Sorry, I unintentionally pasted the wrong part of code. I am currently using a message box, but want a somewhat more beefed out one with some functional buttons.

Thanks a bunch.

like image 671
MLavine Avatar asked Dec 06 '25 09:12

MLavine


2 Answers

How about popping a MessageBox ? Take a look here http://www.dotnetperls.com/messagebox-show

like image 136
graph1ZzLle Avatar answered Dec 08 '25 23:12

graph1ZzLle


All of your events should be wrapped in a code looking like this:

DialogResult result = DialogResult.Retry;
while (result == DialogResult.Retry) {
    try {
        DoProcess();
        break;
    }
    catch (Exception ex) {
        result = MessageBox.Show(ex.ToString(), 
                    "Error Information", 
                    MessageBoxButtons.AbortRetryIgnore,
                    MessageBoxIcon.Exclamation);
        if (result == DialogResult.Abort) throw;
    }
}

Where DoProcess() would be the risky code.

like image 34
Francis P Avatar answered Dec 08 '25 22:12

Francis P