Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Handle Thread Exception

$
0
0

Hi all,

    I have a question about "Handling Thread Exception":

         What is the difference between "Handling each thread exception" and "Handling Application Thread Exception"

         I'm making a test application to test "OutOfMemory Exception". I make about 8 threads to run these function:

        private void btnStart_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 8; i++)
            {
                List<object> processList = new List<object>();
                Thread thread = new Thread(new ThreadStart(() => { TestExceptionWarpper(processList); }));
                thread.Name = string.Format("Test thread#{0}", i);
                thread.IsBackground = false;
                thread.Start();
            }
        }


        private void TestExceptionWarpper(List<object> objects)
        {
            try
            {
                DoTest(objects);
            }
            catch (OutOfMemoryException ex)
            {
                throw;
            }
        }

        private void DoTest(List<object> objects)
        {
            try
            {
                Thread.Sleep(1000);
                while (true)
                {
                    objects.Add(new object());
                }
            }
            catch (OutOfMemoryException oome)
            {
                throw;
            }
            catch (Exception generalException)
            {
                //do something
            }
        }

       and I also register for these two events:

        static void Main()
        {
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormTestPeformance());
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {

        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {

        }

        If I catch exception at  TestExceptionWarpper method, the application is fine. It won't crash.

        But if I throw the exception, the application crash. 

        All 3 handle functions are empty. I don't know what is the difference. And what make the application crash?

        Please help me. Thanks a lot :)




Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>