locked
Exception Handling RRS feed

  • Question

  • A Try-Catch block in 'OnThreadException' does not execute the 'Catch'. Instead an unhandled exception 'Send Report' dialog pops up. What am I missing or doing wrong? The code looks like this:

    static void OnThreadException(object sender, ThreadExceptionEventArgs t){

    try{

    Exception e = t.Exception;

    string message = e.Message;

    if (e.InnerException != null)

    message += Environment.NewLine + Environment.NewLine;

    while (e.InnerException != null)

    {

    message += e.InnerException.Message + Environment.NewLine;

    e = e.InnerException;

    }

    MessageBox.Show(message, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

    }

    catch

    {

    // This code does not execute !!??

    try{

    MessageBox.Show("A critical program error has occured. The application will be shut down and restarted.", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

    }

    finally{

    Application.Restart();

    }

    }

    }

    Friday, March 31, 2006 10:45 AM

Answers

  • What exception is being thrown from inside the try block? You're not doing anything that would, for example, be causing a stack overflow?

    The code is going into the catch block for me, for both debug and release.

    I've got the following:

        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                ErrorHandler er = new ErrorHandler();

                Application.ThreadException += new ThreadExceptionEventHandler(
        er.OnThreadException);

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

        public class ErrorHandler
        {
            public void OnThreadException(object sender, ThreadExceptionEventArgs t)
            {
                try
                {
                    Exception e = t.Exception;
                    string message = e.Message;
                    if (e.InnerException != null)
                        message += Environment.NewLine + Environment.NewLine;
                    while (e.InnerException != null)
                    {
                        message += e.InnerException.Message + Environment.NewLine;
                        e = e.InnerException;
                    }

                    throw new ApplicationException("TODO3");

                    MessageBox.Show(message, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

                catch
                {

                    MessageBox.Show("todo4", "Application Error 2", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    // This code does not execute !!??

                    try
                    {
                        MessageBox.Show("A critical program error has occured. The application will be shut down and restarted.", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }

                    finally
                    {
                        Application.Restart();
                    }
                }
            }
        }

    Form1 just has a button on it which when clicked throws a new ApplicationException.

    the exception with the message "TODO3" is thrown in the try block, and is being caught in the catch block and the "A critical program error..." message is shown.
    Saturday, April 1, 2006 9:36 AM

All replies

  • You wouldn't expect the catch to be executed unless an exception was thrown in the try.

    Is the OnThreadException method being called - have you put a breakpoint in there, and tried getting exceptions to break into the debugger, or is it a release only problem?
    Friday, March 31, 2006 11:27 PM
  • Thanks for the reply. During testing I threw an unhandled exception from from elsewhere and it was caught here.

    But if for some out-of-this-world reason an exception is thrown inside the Try block in the OnThreadException proc it is not being handled as expected at runtime. 

    To reproduce the problem, insert a 'throw new Exception("Exception Handling Exception");' in the Try block in OnThreadException, compile and run the exe.

    Saturday, April 1, 2006 7:46 AM
  • What exception is being thrown from inside the try block? You're not doing anything that would, for example, be causing a stack overflow?

    The code is going into the catch block for me, for both debug and release.

    I've got the following:

        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                ErrorHandler er = new ErrorHandler();

                Application.ThreadException += new ThreadExceptionEventHandler(
        er.OnThreadException);

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

        public class ErrorHandler
        {
            public void OnThreadException(object sender, ThreadExceptionEventArgs t)
            {
                try
                {
                    Exception e = t.Exception;
                    string message = e.Message;
                    if (e.InnerException != null)
                        message += Environment.NewLine + Environment.NewLine;
                    while (e.InnerException != null)
                    {
                        message += e.InnerException.Message + Environment.NewLine;
                        e = e.InnerException;
                    }

                    throw new ApplicationException("TODO3");

                    MessageBox.Show(message, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

                catch
                {

                    MessageBox.Show("todo4", "Application Error 2", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    // This code does not execute !!??

                    try
                    {
                        MessageBox.Show("A critical program error has occured. The application will be shut down and restarted.", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }

                    finally
                    {
                        Application.Restart();
                    }
                }
            }
        }

    Form1 just has a button on it which when clicked throws a new ApplicationException.

    the exception with the message "TODO3" is thrown in the try block, and is being caught in the catch block and the "A critical program error..." message is shown.
    Saturday, April 1, 2006 9:36 AM
  • Thanks for the solution. This actually worked.  I wonder why the static method does not work.
    Saturday, April 1, 2006 2:45 PM
  • I'm glad I helped!

    I'm curious though, as to what exactly you changed to get the program working? - I still got the correct working behaviour after changing OnThreadException to static, and when moving it into the Program class...

    Cheers,
    Gary
    Sunday, April 2, 2006 7:46 PM
  • Sorry to change thread, but how do you paste code segments with nice indents like in your example?

    Thanks

    Monday, April 3, 2006 12:04 AM