简体   繁体   English

无法捕获派生类中引发的异常

[英]Can't catch exception thrown in derived class

Why base class's try-catch doesn't catches exception thrown in derived class? 为什么基类的try-catch不能捕获派生类中引发的异常? Did I missed something? 我错过了什么吗?

Base class: 基类:

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);
            }
        };
    } 
}

Derived class: 派生类:

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);
        }
    }
}

I've read here Can we catch exception from child class method in base class in C#? 我在这里读过我们可以在C#的基类中捕获子类方法的异常吗? and can't find my mistake. 找不到我的错误

Try/Catch will only catch exceptions thrown in the try block. Try / Catch将仅捕获在try块中引发的异常。 That includes any exceptions thrown by other methods called within the try block. 这包括在try块中调用的其他方法引发的任何异常。 Have you got exceptions configured to break on just unhandled or also on thrown ? 您是否已将异常配置为仅在未处理抛出时中断? See here for how to configure exception breaks 请参阅此处以了解如何配置异常中断

The other possibility is that your exception is being thrown at time of object construction, because your ReadParams constructor calls Iteration() without a try/catch. 另一种可能性是在对象构造时抛出了异常,因为ReadParams构造函数在没有try / catch的情况下调用Iteration()。

ie

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);
        }
    };
} 

If I read it right, the sequence is: 如果我没看错,顺序是:

  1. ReadParams ctor ReadParams ctor
  2. UmBase ctor UmBase ctor
  3. ReadParams Iteration ReadParams迭代
  4. ReadParams Iteration throw new Exception("Oops!", exception); ReadParams迭代throw new Exception("Oops!", exception);
  5. Crash... because there is no try-catch in ReadParams ctor 崩溃...因为ReadParams ctor中没有try-catch

When you override a method you actually replace the entire method wholesale as far as instances of the derived class is concerned. override方法时,实际上就涉及派生类的实例而言,整个批发方法都将全部替换。

Unless you call the inherited method explicitly from the overridden one, it is not part of your derived class's logic. 除非您从被重写的方法中显式调用继承的方法,否则它不是派生类逻辑的一部分。

I faced same problem .I noticed one thing but not sure of the reason. 我遇到了同样的问题。我注意到一件事,但不确定原因。 When u inherit a base class privately its catch block does not catch the exception of the derived class. 当u私有地继承基类时,其catch块不会捕获派生类的异常。 inherit the base class publicly and give it a try. 公开继承基类并进行尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法捕获Task引发的异常 - Can't catch Exception thrown from Task 是否有可能捕获派生类构造函数中从基类构造函数抛出的异常 - Is it possible to catch exception thrown from base class constructor inside derived class constructor 捕获另一个类抛出的异常? - Catch exception thrown by another class? 无法捕获Invoke在编译表达式上抛出的异常 - Can't catch exception thrown by Invoke on a compiled expression WebApi无法序列化从Exception派生类声明的属性 - WebApi can't serialize attribute declared on derived class from Exception 为什么我无法捕获第三方库引发的异常? - Why I can't catch an exception thrown from third party library? 无法捕获延迟初始化期间引发的异常(C# .NET) - Can't catch exception thrown during lazy initialization (C# .NET) 如何在另一个类引发的测试类中捕获异常? - How to catch an Exception in test class which is thrown by another class? FaultException异常 <T> ()服务引发的异常未被客户端catch捕获(FaultException <T> ) - FaultException<T>() exception thrown by the service is not caught by the client catch(FaultException<T>) 如何使用C#从派生类中的构造函数中捕获异常? - How to catch exception from a constructor in a derived class with C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM