Cannot catch exception for application

Hi there, long time no update since i am very busy on my work. :)

Some days before, i meet a problem about catch exception in .NET . Let me show the sample coding 1st, then we discuss what’s the fuck….

     static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                Application.Run(new Form1());
            }
            catch (Exception e)
            {

                MessageBox.Show(“Error”);
            }
        }
    }

It’s a main method of window application. I want to catch any unhandle exception of the windows application. It’s worked when i run the problem in VS.NET IDE. However, when i run the executable file in bin, the exception cannot be captured. It prompt me a exception message box like a exception i haven’t handled. I am so strange why this happen since i have try and catch to handle all exception. I have no idea why this happen. Therefore, i search from google and MSDN forum. I found that if we need to capture the exception of throw at Application, you have to Listen the event of Application.ThreadException. Thus, i rewrite my code to listen the event. Although it works at executable of the application, it doesn’t work at IDE mode.

I have no idea why there is different behaviour between IDE mode and execuable file. However, i think it is so trouble for developer since it is hard to debug. =.=

If you have any idea or suggestions, please let me know. Thanks

Advertisement

2 Responses to Cannot catch exception for application

  1. william says:

    Totally agree with you, MS sucks.
    But I think you might be able to use some sort of conditional compilation to solve this issue… somehow to find a way to distinguish between the VS design time execution and the real runtime, and choose the appropriate routine.

  2. william says:

    Yeah, you are really too lazy to update your blog! Just to remind you :P

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.