代码之家  ›  专栏  ›  技术社区  ›  Budda

如何避免.NET中的复制粘贴异常

  •  7
  • Budda  · 技术社区  · 15 年前

    [WebMethod]
    void Method1(...)
    {
        try
        {
            ... required functionality
        }
        catch(MyException2 exc)
        {
            ... process exception of MyException2 type
        }
        catch(MyExc1 exc)
        {
            ... process exception of MyExc1 type
        }
        catch(Exception exc)
        {
            ... process exception of Exception type
        }
        ... process and return result if necessary
    }
    

    在每个服务方法中有完全相同的内容(每个方法有不同的参数集)和完全相同的异常处理功能是非常无聊的。。。

    是否有可能“分组”这些catch部分,只使用一行(类似C++宏)?可能.NET4.0中的一些新内容与此主题有关?

    谢谢。

    附言:欢迎有任何想法。

    7 回复  |  直到 15 年前
        1
  •  6
  •   Reed Copsey    15 年前

    完全一样 在您的所有方法中,您可以执行以下操作:

    void CallService(Action method)
    {
        try
        {
            // Execute method
            method();
        }
        catch(MyException2 exc)
        {
            ... process exception of MyException2 type
        }
        catch(MyExc1 exc)
        {   
            ... process exception of MyExc1 type
        }
        catch(Exception exc)
        {
            ... process exception of Exception type
        }
    }
    

    然后,您可以重写客户端代码:

    int i = 3;
    string arg = "Foo";
    this.CallService( () => this.Method1(i) );
    this.CallService( () => this.Method2(arg, 5) );
    

    这使得Method1和Method2方法可以简单地:

    void Method1(int arg)
    {
        // Leave out exception handling here...
        ... required functionality  
        ... process and return result if necessary
    }
    
    void Method2(string stringArg, int intArg)
    {
        // Leave out exception handling here...
        ... required functionality  
        ... process and return result if necessary
    }
    
        2
  •  5
  •   dcp    15 年前

    HandleException 同时,这使得它具有很强的可扩展性)?

    try
    {
        ... required functionality
    }
    catch (Exception e)
    {
        HandleException(e);
        throw; // only if you need the exception to propagate to caller
    }
    
    
    private void HandleException(Exception e)
    {
        if (e is MyException2)
        {
            ... process exception of MyException2 type
        }
        else if (e is MyExc1)
        {
            ... process exception of MyExc1 type
        }
        else
        {
            ... process exception of Exception type
        }
    }
    
        3
  •  2
  •   John Saunders    15 年前

        4
  •  1
  •   Arne    15 年前

    在一个突然闪现的怪癖(并显示什么你可以,但最有可能不应该和不想做):你可以让整个事情更可组合和可重用的功能在飞行中生成包含异常处理逻辑:

    static class ExceptionHandlerExtensionMethods 
    {
        // extend to Func<T> as desired
        public static Action Catching<T>(this Action what, Action<T> handler) 
            where T : Exception
        {
            return () =>
            {
                 try
                 {
                     what();
                 }
                 catch (T ex)
                 {
                     handler(ex);
                 }
             };
        }
    }
    

    现在您可以在某处实现和重用特定于异常类型的处理函数,并将它们组合到其他方法的异常处理中。

        5
  •  0
  •   Francis Gagné    15 年前

    使用Reed Copsey的CallService方法:

    void DoSomething(object param1, int param2)
    {
        this.CallService(() =>
        {
             // work with param1 and param2 here
        }
    }
    

    T CallService<T>(Func<T> callback) { /* ... */ }
    
        6
  •  -3
  •   Damien Dennehy    15 年前

    我知道这是不好的做法,但是如果在每个catch语句中都有完全相同的错误处理,那么为什么不将最后一个catch语句用作catch all呢?

    当然,这假设您的所有异常都继承自异常。