0

I'm trying to catch unhandled exception this way :

static class Program
{
        [STAThread]
        static void Main(string[] args)
        {
            Application.ThreadException += new ThreadExceptionEventHandler(Program.ThreadExceptionEventHandler);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Program.UnhandledExceptionEvent);

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

        public static void UnhandledExceptionEvent(Object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show("UnhandledExceptionEvent", "UnhandledExceptionEvent");
        }

        public static void ThreadExceptionEventHandler(Object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message, "ThreadExceptionEventHandler");
        }
}

private void button1_Click(object sender, EventArgs e)
        {
            //Execute method on a new thread
            new Thread(delegate()
            {
                //Do stuff ...
                throw new Exception("Some random unhandled exception");

            }).Start();

        }

The exception is caught by the UnhandledExceptionEventHandler, I can see the message box popping, but the application still crashes saying "Program has stopped working".

How do I keep the application runnning after an exceptions occurs ?

KVM
  • 878
  • 3
  • 13
  • 22
  • Normally you would surround your code block with a try - catch block.The exception would be specific to what might occur and if you're not sure just use the Exception class. – Mukus Mar 09 '14 at 23:34
  • I believe the "stopped working" message has something to do with another error. – Mauren Mar 09 '14 at 23:35
  • 1
    @Tejaswi Rana If I surround with a try-catch block then it isn't an unhadled exception anymore, it's handled, which obviously won't crash the application, but it's not my question. – KVM Mar 09 '14 at 23:39
  • @Xenolightning There is no such property, please see youself : http://msdn.microsoft.com/en-us/library/system.unhandledexceptioneventargs(v=vs.110).aspx – KVM Mar 09 '14 at 23:45
  • @Mauren I did a new project with just the code above, nothing else – KVM Mar 09 '14 at 23:47
  • @Michael Look at the last answer here: http://stackoverflow.com/questions/5049063/unhandled-exception-in-winforms-application?rq=1 – Mukus Mar 09 '14 at 23:51
  • @Michael isn't there a case where `ThreadExceptionEventHandler` will receive a `null` `ThreadExceptionEventArgs`? – Mauren Mar 09 '14 at 23:52
  • @Tejaswi Rana the first answer actually solves my issue. Please post this as an answer, I will mark it as the solution. – KVM Mar 10 '14 at 00:05
  • @Mauren ThreadExceptionEventHandler is not firing in this example – KVM Mar 10 '14 at 00:09
  • Why not just fix the problem that is causing the unhandled exception? – John Saunders Mar 10 '14 at 00:10
  • @Michael I don't think I should take credit for someone else's answer.. and that too on stackoverflow. – Mukus Mar 10 '14 at 00:23
  • @Tejaswi Rana You are not stealing anyone's anwser, you are just answering another question. I think giving an answer in the comment section is a Stack Overflow "bad practice" – KVM Mar 10 '14 at 00:27
  • cool. I will add it then. – Mukus Mar 10 '14 at 01:36

1 Answers1

2

Please see this stackoverflow question for information related to catching unhandled excpetions.

Community
  • 1
  • 1
Mukus
  • 4,870
  • 2
  • 43
  • 56