无法捕获派生类中抛出的异常

时间:2014-12-04 22:43:25

标签: c# exception-handling derived-class base-class

为什么基类的try-catch不会捕获派生类中抛出的异常? 我错过了什么吗?

基类:

public class UmBase
{
    protected Thread ThisThread;

    protected UmBase(int cycleMs, UpdateManager updateManager,
                     string loggerFilename, string loggerFolder = "UpdateManager")
    {
    }

    public void Start()
    {
        ThisThread = new Thread(Work);
        ThisThread.Start();
    }

    public virtual void Iteration()
    {
        throw new Exception("Iteration Method should be overidden!");
    }

    public void Work()
    {
        while (IsProcessing)
        {
            try
            {
                Iteration();
            }
            catch (Exception exception)
            {
                Log.Error(exception.Message); //WANT TO HANDLE IT HERE
            }
            finally
            {
                Sleep(100);
            }
        };
    } 
}

派生类:

public class ReadParams : UmBase
{
    public ReadParams(UpdateManager updateManager, int cycleMs = 60000)
        : base(cycleMs, updateManager, "sss")
    {
        Iteration();
    } 

    public override void Iteration()
    {
        try
        {
            DbParams.Set(); //EXCEPTION IS THROWN INSIDE
        }
        catch (Exception exception)
        {
            throw new Exception("Oops!", exception);
        }
    }
}

我在这里阅读Can we catch exception from child class method in base class in C#?并找不到我的错误。

4 个答案:

答案 0 :(得分:3)

Try / Catch只会捕获try块中抛出的异常。这包括try块中调用的其他方法抛出的任何异常。您是否已将异常配置为仅仅未处理抛出See here for how to configure exception breaks

另一种可能性是在构造对象时抛出异常,因为你的ReadParams构造函数在没有try / catch的情况下调用Iteration()。

即。

public class ReadParams : UmBase
{
    public ReadParams(UpdateManager updateManager, int cycleMs = 60000)
        : base(cycleMs, updateManager, "sss")
    {
        Iteration();
    } 

    public override void Iteration()
    {
        try
        {
            // If throw here (A)
            DbParams.Set(); //EXCEPTION IS THROWN INSIDE
        }
        catch (Exception exception)
        {
            // I'll catch here (A) and then throw a new exception
            throw new Exception("Oops!", exception);
        }
    }
}

public void Work()
{
    while (IsProcessing)
    {
        try
        {
            // Exceptions thrown here including the one you 
            // threw in the method Iteration (B)
            Iteration();
        }
        catch (Exception exception)
        {
            // Will be caught here (B)
            Log.Error(exception.Message); //WANT TO HANDLE IT HERE
        }
        finally
        {
            Sleep(100);
        }
    };
} 

答案 1 :(得分:2)

如果我读得正确,顺序是:

  1. ReadParams ctor
  2. UmBase ctor
  3. ReadParams Iteration
  4. ReadParams Iteration throw new Exception("Oops!", exception);
  5. 崩溃......因为ReadParams ctor
  6. 中没有try-catch

答案 2 :(得分:0)

当你override一个方法时,你实际上替换整个方法批发,就派生类的实例而言。

除非您从重写的方法中显式调用继承的方法,否则它不是派生类逻辑的一部分。

答案 3 :(得分:-1)

我遇到了同样的问题。我注意到了一件事,但不确定原因。 当你私下继承一个基类时,它的catch块不会捕获派生类的异常。 公开继承基类并试一试。