要求以下3个处理抛异常的时候,调用同一个处理(形如MyException),不希望在每个方法里加try-catch。
请教该怎么实现。        private void MyException(Exception e)
        {
            Console.WriteLine(e.ToString());
        }-----------------------------------以下3个方法,参数,返回值不一样-----
        private void Test1(string a1)
        {
            //业务处理
        }        private void Test2()
        {
            //业务处理
        }        private string Test3(string a2,string a3)
        {
            //业务处理
            return "";
        }
----------------------------------------------------------------------------

解决方案 »

  1.   

    那就在每个函数中用throw抛出自定义的异常,或者根据数据是否符合规则调用  MyException,  MyException中可以throw异常或者捕获异常
      

  2.   

    在调用三个方法的区间用try catch
      

  3.   

         static void Main(string[] args)
            {            Test1("1");
                Test2();
                Test3("1","2");            Console.ReadLine();
            }        /// <summary>
            /// 捕获下列3个Test方法异常
            /// </summary>
            /// <param name="e"></param>
            private void MyException(Exception e)
            {
                Console.WriteLine(e.ToString());
            }        private static  void Test1(string a1)
            {
                //业务处理
                if (true)
                {            }
                else
                {
                    throw new Exception("");
                }
            }        private static void Test2()
            {
                //业务处理
                if (true)
                {
                     
                }
                else
                {
                    throw new Exception("");
                }
            }        private static string Test3(string a2, string a3)
            {
                //业务处理
                if (true)
                {
                    return "";
                } else
                {
                    throw new Exception("");
                }
            }
      

  4.   

    非常感谢版主的回复,若按照以下方式,代码量将大大增加的。我不想try-catch每个所调之处。是否可通过委托或事件方式实现。        static void Main(string[] args)
            {            try
                {
                    Test1("1");
                }
                catch (Exception)
                {                throw;
                }            try
                {
                    Test2();
                }
                catch (Exception)
                {                throw;
                }            try
                {
                    Test3("1", "2");
                }
                catch (Exception)
                {                throw;
                }
                Console.ReadLine();
            }
      

  5.   

    楼主加个 try 有多少代码,委托,事件不也是要写代码的么?
      

  6.   

    如果try-catch包含的代码较多,会显得比较臃肿,所以有没比较好的办法呢。
      

  7.   


    private static void GuardedExec(Action action)
    {
        try
        {
            action();
        }
        catch (Exception e)
        {
            MyException(e);
        }
    }static void Main()
    {
        GuardedExec(() => 
        {
            Test1("1");
        });    GuardedExec(() =>
        {
            Test2();
        });    string result = "";
        GuardedExec(() =>
        {
            result = Test3("1", "2");
        });
    }不想用Lambda的话,也可以用匿名方法:
        GuardedExec((Action)delegate
        {
            Test1("1");
        });
      

  8.   

    throw new MyException(Exception e)就行了
      

  9.   

    谢谢gomoku。
    这个方法我也用了,不知有没更好的方式,即在调用方法的方法体外,可否定义某个XX,自动捕获此方法抛出的异常。
      

  10.   

    throw new MyException(Exception e)这个不行?
      

  11.   

    to u011130289:
    捕获的异常是未知的。