hello every one
i want to ask if we can implement global catch exception
for the whole application so that any exception that occur , will be handled without closing the form specially after making setup for the application (then i can't determine where the exception came from)


Short answer: No.

you could write try catch bocks around all the code you write, and then create a method tha takes an exception param, and call it in all the catch blocks passing to it the exception that caused the problem. BUT its not a good idea. try catch is used only when there is the chance that there will be an error that you can't handle manually. Most errors should be prevented using if or switch blocks. try catch is slow. you can't sandbox your entire app without adding an overhead.

Not sure what you mean by "after making setup" but you can put a try catch block in your Main() method:

static class Program
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
                Application.Run(new Form1());
            catch(Exception ex)
                  //process exception here

But generally a global catch all is ill advised. You should generally use individual try/catch blocks around small sections of code that could potentially raise an exception (such as file handling methods), and only if throwing an exception is actually exceptional. Check out Best Practices for Handling Exceptions.

This article has been dead for over six months. Start a new discussion instead.