【问题标题】:Handle base class exception处理基类异常
【发布时间】:2011-04-05 12:48:30
【问题描述】:

我有以下 C# 场景- 我必须处理派生类中实际发生的基类中的异常。 我的基类如下所示:

public interface A
{
    void RunA();
}
public class Base
    {
        public static void RunBase(A a)
        {
            try
            {
                a.RunA();
            }
            catch { }
        }
    }

派生类如下:

public class B: A
{
        public void RunA()
        {
            try
            {
                //statement: exception may occur here
            }
            catch{}
    }
}

我想处理异常,比如说异常 C,它发生在 B(在上面的 // 语句中)。 异常处理部分应该写在 RunBase 中的基类 catch 中。如何做到这一点?

【问题讨论】:

    标签: c# inheritance exception-handling


    【解决方案1】:
    public class Base
    {
        public static void RunBase(A a)
        {
            try
            {
                a.RunA();
            }
            catch(SomeSpecialTypeOfException ex)
            { 
                // Do exception handling here
            }
        }
    }
    
    public class B: A
    {
        public void RunA()
        {
            //statement: exception may occur here
            ...
    
            // Don't use a try-catch block here. The exception
            // will automatically "bubble up" to RunBase (or any other
            // method that is calling RunA).
        }
    }
    

    【讨论】:

      【解决方案2】:

      如何做到这一点?

      什么意思? 只需从RunA 中删除 try-catch 块。

      话虽如此,您需要确保 A 类知道如何处理异常,这包括将其简化为 UI、日志记录……这实际上 很少见 strong> 用于基类。处理异常通常发生在 UI 级别。

      【讨论】:

        【解决方案3】:
        public class B: A
        {
                public void RunA()
                {
                    try
                    {
                        // statement: exception may occur here
                    }
                    catch(Exception ex)
                    {
                        // Do whatever you want to do here if you have to do specific stuff
                        // when an exception occurs here
                        ...
        
                        // Then rethrow it with additional info : it will be processed by the Base class
                        throw new ApplicationException("My info", ex);
                    }
            }
        }
        

        您可能还想按原样抛出异常(单独使用throw)。

        如果这里不需要处理任何东西,就不要放try{} catch{},让异常自行冒泡,由Base类处理。

        【讨论】:

          【解决方案4】:

          只需从 B 类中删除 try catch,如果发生异常,它将在调用链上进行适当的处​​理,直到它被处理为止。在这种情况下,您可以使用现有的 try catch 块处理 RunBase 中的异常。

          虽然在您的示例中 B 不是从您的基类 Base 派生的。如果您真的想处理在其父类的派生类中引发异常的情况,您可以尝试以下操作:

          public class A
          {
              //Public version used by calling code.
              public void SomeMethod()
              {
                  try
                  {
                      protectedMethod();
                  }
                  catch (SomeException exc)
                  {
                      //handle the exception.
                  }
              }
          
              //Derived classes can override this version, any exception thrown can be handled in SomeMethod.
              protected virtual void protectedMethod()
              {
              }
          
          }
          
          public class B : A
          {
              protected override void protectedMethod()
              {
                  //Throw your exception here.
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-02-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多