locked
Catch all Exceptions RRS feed

  • Question

  • Hi

    I have a program with several Forms.
    What I need is to log all exceptions (save them into a database).

    Is this possible without having to write a "try{ ... } catch(...) {...}" everywhere? (and call a static log-method within the catch-body).

    Kind Regards
    Saturday, July 22, 2006 10:47 AM

Answers

  • One of the easiest ways is to do so within your Main method at the start of the program and cause it to end up looking like so:

          static void Main()

          {

             Application.EnableVisualStyles();

             Application.SetCompatibleTextRenderingDefault(false);

             Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

             try

             {

                Application.Run(new Form1());

             }

             catch (Exception ex)

             {

                Application_ThreadException(null, new System.Threading.ThreadExceptionEventArgs(ex));

             }

          }

     

          static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)

          {

             //Do reporting/logging/etc of exception heres

          }

    Here we setup an event handler for the ThreadException event while also throwing our main app execution into try/catch block so that any unhandled exceptions within should be caught and end up in Application_ThreadException().

    Is this what you are looking for?

    Saturday, July 22, 2006 3:25 PM
  • Well, the best way to be able to catch every exception in your application is by running it in debug mode via Visual Studio. When it runs into a(n) exception, it will display the error, and the type of exception it is. Then you can just implement the code for that particular exception.

    So if you select a value of -1 and it presents you with an exception, run it in debug mode and find out what exception it is, then implement the catch statement.

    What I would do is implement this in the dataGridViews "CellClick" event or something similir. I am not sure what event you would actually need to implement the exception handler in, but that should give you an idea on how to do it.

    As for why his code did not work I would not know.  I always prevent from adding any unneccessary code in the main source code file.
    Sunday, July 23, 2006 12:25 AM

All replies

  • One of the easiest ways is to do so within your Main method at the start of the program and cause it to end up looking like so:

          static void Main()

          {

             Application.EnableVisualStyles();

             Application.SetCompatibleTextRenderingDefault(false);

             Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

             try

             {

                Application.Run(new Form1());

             }

             catch (Exception ex)

             {

                Application_ThreadException(null, new System.Threading.ThreadExceptionEventArgs(ex));

             }

          }

     

          static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)

          {

             //Do reporting/logging/etc of exception heres

          }

    Here we setup an event handler for the ThreadException event while also throwing our main app execution into try/catch block so that any unhandled exceptions within should be caught and end up in Application_ThreadException().

    Is this what you are looking for?

    Saturday, July 22, 2006 3:25 PM
  • Hi

    I actually tried this a while ago.
    The problem is that the main program exits then.

    And I tried to select a negative row in a DataGridView.
    The program did crash :), but the exception was not taken care of.
    Saturday, July 22, 2006 3:42 PM
  • It all depends on how you want to handle each exception that has the possibility to ocurr.  For example, say you have a processControl and it throws a FileNotFoundException, (wrong exception, but just an example).  And you want to display a MessageBox saying to make sure the application is installed, handling everything in main() wouldn't give you that functionality.

    If I am wrong, yell at me for being too lazy to open up Visual Studio and try.  :P
    Saturday, July 22, 2006 3:48 PM
  • No, you're not wrong :)

    Of course I display custom MessageBoxes for a bunch of Exceptions :)
    If I think again, it's ok if the program exits when an unhandled exception occurs.

    But why didn't Brendan's example catch the exception when I selected a negative row in the DataGridView?

    I got an "ArgumentException was unhandled by user code" message.
    Saturday, July 22, 2006 3:56 PM
  • Well, the best way to be able to catch every exception in your application is by running it in debug mode via Visual Studio. When it runs into a(n) exception, it will display the error, and the type of exception it is. Then you can just implement the code for that particular exception.

    So if you select a value of -1 and it presents you with an exception, run it in debug mode and find out what exception it is, then implement the catch statement.

    What I would do is implement this in the dataGridViews "CellClick" event or something similir. I am not sure what event you would actually need to implement the exception handler in, but that should give you an idea on how to do it.

    As for why his code did not work I would not know.  I always prevent from adding any unneccessary code in the main source code file.
    Sunday, July 23, 2006 12:25 AM
  • Ok, thanks
    Sunday, July 23, 2006 9:02 AM
  • i have seen ThreadException and the other related to AppDomain exception but is it really useable for windows apps for unhandled exception scenarios?

    i'd rather catch it in the proper places where i expect them to be raised, in my opinion.
    Sunday, July 23, 2006 1:33 PM