Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Grabbing Unhandled Exceptions in Winforms (C#)
Message
De
15/03/2008 12:44:45
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01301455
Message ID:
01302400
Vues:
18
>Use the Application.ThreadException() to set up a new handler. You can also use the Application.SetUnhandledExceptionMode() to set up a handler as well.
>
>Of course you should have try{}...catch{} around sections of code and not only rely on these global error handlers.


Hey Derek,

Have you actually gotten this to work correctly? I looked up info on Application.ThreadException() in the docs and copied the example into my test app. I have a button that throws an Exception, without a try/catch ... I end up getting two errors dialogs getting displayed.

One from the actual thrown exception, the dialog that .NET would normally show when an Exception is detected and there is no try/catch. And then the Dialog that I've coded in the Exception Handler. This is not exactly what I expected to happen. Have you discovered something different?

Here's what I have:
[STAThread]
static void Main()
{
	// Creates an instance of the methods that will handle the exception.
	CustomExceptionHandler eh = new CustomExceptionHandler();

	// Adds the event handler to to the event.
	Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);

	Application.Run(new Form1());
}

// The following was copied directly from the docs
#region Exception Handling
// Creates a class to handle the exception event.
internal class CustomExceptionHandler
{

	// Handles the exception event.
	public void OnThreadException(object sender, ThreadExceptionEventArgs t)
	{
		DialogResult result = DialogResult.Cancel;
		try
		{
			result = this.ShowThreadExceptionDialog(t.Exception);
		}
		catch
		{
			try
			{
				MessageBox.Show("Fatal Error", "Fatal Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
			}
			finally
			{
				Application.Exit();
			}
		}

		// Exits the program when the user clicks Abort.
		if (result == DialogResult.Abort)
			Application.Exit();
	}

	// Creates the error message and displays it.
	private DialogResult ShowThreadExceptionDialog(Exception e)
	{
		string errorMsg = "An error occurred please contact the adminstrator with the following information:\n\n";
		errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
		return MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
	}
}
#endregion

// And then the button click that simply throws an exception:
	throw new Exception("Test Exception Handling");
I've also tried it with the try/catch in the Main, but it still does the same thing:
[STAThread]
static void Main()
{
	try
	{
		// Creates an instance of the methods that will handle the exception.
		CustomExceptionHandler eh = new CustomExceptionHandler();

		// Adds the event handler to to the event.
		Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);

		Application.Run(new Form1());
	}
	catch (Exception ex)
	{
		MessageBox.Show(ex.Message);
	}
}
Did I miss something? Like I said, this is exactly how the docs say to do it. I'm assuming you played around with it a bit more and got it to work properly.

~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform