You can use Application_ThreadException event to handle all exceptions globally. You can find more about it here
[STAThread] static void Main() {
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e) {
throw new Exception("Whoops");
}
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
MessageBox.Show(e.Exception.Message);
}
To show user friendly messages on exception you can check this codeproject article.
You can use Exception Handling Application Block from Microsoft also.The Introduction at MSDN says as below :
The Enterprise Library Exception Handling Application Block lets developers and policy makers create a consistent strategy for processing exceptions that occur in all architectural layers of an enterprise application. It does this in the following ways:
- It supports exception handling in all architectural layers of an application and is not limited to service interface boundaries.
- It allows exception handling policies to be defined and maintained at the administrative level so that policy makers, who might be system administrators as well as developers, can define how to handle exceptions. They can maintain and modify the rules that govern exception handling without changing the application block code.
- It provides commonly used exception handling functions, such as the ability to log exception information, the ability to hide sensitive information by replacing the original exception with another exception, and the ability to maintain contextual information for an exception by wrapping the original exception inside of another exception. These functions are encapsulated in .NET classes named exception handlers.
- It can combine exception handlers to produce the desired response to an exception, such as logging exception information followed by replacing the original exception with another.
- It lets developers create their own exception handlers.
- It invokes exception handlers in a consistent manner. This means that the handlers can be used in multiple places within and across applications.