簡體   English   中英

無法捕獲派生類中引發的異常

[英]Can't catch exception thrown in derived 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);
        }
    }
}

我在這里讀過我們可以在C#的基類中捕獲子類方法的異常嗎? 找不到我的錯誤

Try / Catch將僅捕獲在try塊中引發的異常。 這包括在try塊中調用的其他方法引發的任何異常。 您是否已將異常配置為僅在未處理拋出時中斷? 請參閱此處以了解如何配置異常中斷

另一種可能性是在對象構造時拋出了異常,因為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. ReadParams ctor
  2. UmBase ctor
  3. ReadParams迭代
  4. ReadParams迭代throw new Exception("Oops!", exception);
  5. 崩潰...因為ReadParams ctor中沒有try-catch

override方法時,實際上就涉及派生類的實例而言,整個批發方法都將全部替換。

除非您從被重寫的方法中顯式調用繼承的方法,否則它不是派生類邏輯的一部分。

我遇到了同樣的問題。我注意到一件事,但不確定原因。 當u私有地繼承基類時,其catch塊不會捕獲派生類的異常。 公開繼承基類並進行嘗試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM