Reputation: 16345
I'm handling all of my unhanded exception in the code but whenever one happens (not during debugging) I get my error window and as soon as it closes "Unhandled application exception has occurred in your application" window pops up. How do I suppress it?
PS : I am not using ASP.NET , I'm using Windows Forms
Upvotes: 4
Views: 5835
Reputation: 942508
You cannot suppress AppDomain.UnhandledException
. Something really nasty happened, a thread in your program died from a heart attack. The odds that the program will continue to run in a meaningful way are zero, .NET puts an end to the misery by terminating the program.
Write an event handler for the AppDomain.CurrentDomain.UnhandledException
event and log or display the value of e.ExceptionObject.ToString()
so you know what caused the mishap. That gives you a hint how to fix your code. If any, it may well be something that you cannot fix yourself. Some kind of database server malfunction for example. Doing anything to intentionally hide the error is therefore a Very Bad Idea. Your customer's support staff will have no idea what to do.
Upvotes: 4
Reputation: 1467
Here's a solution to show all unhandled exceptions, both managed and unmanaged types:
static void Main() {
try {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException +=
new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.Run(new Form1());
}
catch (Exception ex) {
MessageBox.Show("A fatal error has occurred. Please contact your admin\n" +
"Exception type: " + ex.GetType() + "\n" +
"Exception Message: " + ex.Message + "\n" +
ex.StackTrace, "Fatal Exception");
if (ex.InnerException != null) {
MessageBox.Show("Inner Exception:\n" +
"Exception type: " + ex.InnerException.GetType() + "\n" +
"Exception Message: " + ex.InnerException.Message + "\n" +
ex.StackTrace, "Fatal Exception (inner)");
}
}
}
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
throw new Exception(e.Exception.Message, e.Exception);
}
To test this, I used C++/CLI to throw an unmanaged exception. These won't get caught properly unless the above Application_ThreadException Handler is present.
Upvotes: 1
Reputation: 131
For Windows Forms, you can send unhandled exceptions (that would otherwise cause the unhandled exception window to pop up) to your exception handler by adding the following in the Main()
method, before the Application.Run()
method is called:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
You can subscribe your handler to the unhandled exception event by adding the following to the form's constructor or elsewhere:
Application.ThreadException += myHandler;
And your form's handler method would look like this:
void myHandler(object sender, System.Threading.ThreadExceptionEventArgs e)
{
//do something
}
You can find more information about this on the msdn.
Upvotes: 4
Reputation: 58632
I would use exception shielding.
This one shows use for Services
Its part of the enteprise library, and allows you to filter out exceptions as well as remap them, to hide the details.
Upvotes: 0