0

I want my exe to just close if any unhandle exception occurs. I have tried How can I make something that catches all 'unhandled' exceptions in a WinForms application? and also Terminate application after unhandled exception and several other posts i found in stackoverflow> But to no effect i could close the programme without showing the messagebox "Unhandled exception has occured in your application. If you click continue, the application will ignore this error and attempt to continue If you click quit ,the application will close immediately".

I want to quit the application but i dont want this message to be shown. Help is much appreciated.

The code:

[STAThread]
        static void Main()
        {


           //   Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

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

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());


        }
       public static void MyHandler(object sender, UnhandledExceptionEventArgs e)
        {
            Exception exception = e.ExceptionObject as Exception;
            System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess();
            Application.Exit();
            proc.Kill();

        }

on getting exception it shows this messagebox. Cant really add it as i dont have reputation point. Jus now opened the account.

I dont want this message box to be shown and dont want to click the quit button to quit the program. Just want to quit on getting any unhandled exception.

Community
  • 1
  • 1
Bony Amin
  • 21
  • 2
  • seems like it is a problem with your code or 3rd party code. this normally will never happen. post your code. even better would be to see what happens in the debugger when you run your app. perhaps even add logging and show us the exception being given in the JIT dialog – Ahmed ilyas Jan 20 '14 at 18:46
  • 1
    Just wrap a try/catch on the main entry point and exit the app in the catch logic. All exceptions at that point should bubble to the top. – Botonomous Jan 20 '14 at 18:47
  • This is just band-aiding broken code...you'd be much MUCH better off digging through and finding the exception, and fixing it. – user2366842 Jan 20 '14 at 19:02
  • I just modified code for win forms. This is how you handle rest of (unhandled) exceptions in Win Forms. See my answer – T.S. Jan 20 '14 at 19:34

3 Answers3

4

Check if this works

public static void Main()   
{
   Application.ThreadException += HandleAll();
   Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
}



private static void HandleAll(object sender, System.Threading.ThreadExceptionEventArgs e)   
{
   Application.Exit();
}

If you expect multiple threads in you application than you can do:

AppDomain.CurrentDomain.UnhandledException += .....
T.S.
  • 18,195
  • 11
  • 58
  • 78
1

Put a try-catch block around the startup logic in Main.

public static void Main()
{
    try
    {
        // run the application
    }
    catch
    {
        // ignore any exceptions (FYI...this is bad practice)
    }
} 
Matt Davis
  • 45,297
  • 16
  • 93
  • 124
1

Application.Exit should be enough you shouldn't need to also kill the process.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Andy
  • 8,432
  • 6
  • 38
  • 76