2

I want a general catch all error that works for all functions. Right now I have a try and catch for each individual button click for specific events. I need a catch all error for my whole program which includes all button clicks that can catch any individual exception I might have missed. Can someone please point me in the right direction

My current code:

private void Show_btn_Click(object sender, EventArgs e)
{
  try
 {
   //do something
 }
 catch (Exception error)
 {
    outputLOG.Append(error.ToString());
 {
}

private void Submit_btn_Click(object sender, EventArgs e)
{
  try
 {
   //do something
 }
 catch (Exception error)
 {
    outputLOG.Append(error.ToString());
 }
}

Goal: I want to catch ALL buttons just in case I missed individual exceptions

EDIT: I am using winforms

LarsTech
  • 80,625
  • 14
  • 153
  • 225
SlopTonio
  • 1,105
  • 1
  • 16
  • 39
  • Check these. http://stackoverflow.com/questions/82483/how-to-catch-all-exceptions-crashes-in-a-net-app http://stackoverflow.com/questions/219594/net-whats-the-best-way-to-implement-a-catch-all-exceptions-handler http://stackoverflow.com/questions/5762526/how-can-i-make-something-that-catches-all-unhandled-exceptions-in-a-winforms-a – peter.petrov Dec 26 '13 at 22:00

1 Answers1

0

Add these lines before entering the first form of your application (usually in the main method in program.cs)

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
......

private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    string msg = e.Exception.Message;
    if (e.Exception.InnerException != null)
    {
        msg = msg + "\r\nPrevious error:" + e.Exception.InnerException.Message;
    }
    msg = msg + "\r\n\r\nDo you wish to continue with the application?";
    DialogResult dr = MessageBox.Show(msg, "Exception", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (dr == DialogResult.No) Application.Exit();
}

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Exception ex = e.ExceptionObject as Exception;
    if (ex != null)
    {
        string msg = ex.Message;
        if (ex.InnerException != null)
        {
            msg = msg + "\r\nPrevious error :" + ex.InnerException.Message;
        }
        msg = msg + "\r\n\r\nIt is not possible to continue. Contact support service!";
        MessageBox.Show(msg, "Fatal Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • im getting this error: SetUnhandledExceptionMode is a method and being used as a type – SlopTonio Dec 26 '13 at 22:22
  • Probably some kind of error before that line. See the [MSDN reference](http://msdn.microsoft.com/en-us/library/ms157905(v=vs.110).aspx). I have no idea why the compiler thinks about a type – Steve Dec 26 '13 at 22:35