Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a way to cut down on the individual exception handling code placed in classes and methods so it can be centralized but not result in termination.

I can see that you can use program.cs and put a try/catch around Application.Run. The exception can be caught. This provides a basis for logging and informing the user, but the application still terminates after the catch logic.

You can put an Application.Run in the catch and get it to start over one more time, but the next exception cannot be caught. So its not much use.

The solution I am trying to determine would look something like:

Form1.cs:

C#
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
           InitializeComponent();
        }

        private void testMethod()
        {
            throw new ArgumentException("Invalid parameter");
        }
      
        private void nextMethod()
        {
          // More Code
        }
    }
}

Program.cs:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]

        static void Main()
        {
         try
         {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
         }

         catch
         {
           // Log Exception, Inform User.
           // Return to Previous Level and continue.
         } 
    }
}


The return to previous level and continue does not seem possible. I am then left with sprinkling exception handling code throughout the application and replicating catch blocks to some degree.

It seems to me that there must be a way to create a centralized exception handler and reduce the number of try/catch blocks overall.

I have read quite a few articles but none seem to address this issue. I realize I can try to reduce the exception handling with logic and rethrow exceptiosn, etc. But this still leaves the issue open for me.

Am I missing something?

Any ideas welcome.

Thanks.
Posted
Updated 23-Feb-16 4:57am
v2
Comments
#realJSOP 19-Nov-10 7:35am    
I'm reposting this question in the C# forum. The forums are better for threads than questions/answers because there is no real mechanism here for responding to answers.
dpminusa 19-Nov-10 7:39am    
Thanks. I am still learning to get the most from the codeproject resources.

1 solution

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900