簡體   English   中英

從方法向前捕獲

[英]Forward catch from method

我正在嘗試獲取網絡設備的ComponentId,所有這些都可以,但是現在我試圖從GetKey()方法獲取一個捕獲。 我只是不太確定如何轉發它(或者這是否是正確的方法)。 我什至應該在方法中使用try / catch嗎?

static string GetKey()
{
    try
    {
        using (RegistryKey Adapter = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}", true))
        {
            foreach (string Keyname in Adapter.GetSubKeyNames())
            {
                RegistryKey Key = Adapter.OpenSubKey(Keyname);
                if (Key.GetValue("DriverDesc").ToString() != "somename")
                { 
                     return Key.GetValue("ComponentId").ToString();
                }
                return null;
             }
             return null;
         }
     }
     catch (Exception ex)
     {
         return ex.Message;
     }
}


static void Main(string[] args)
{
    if (GetKey() == null)
    {
        Console.WriteLine("null");
    }
    else if () // if catch throws exception?
    {}
    else
    {
        Console.WriteLine(GetKey());
    }
    Console.ReadKey();
}

對我來說,在返回字符串(這是鍵)的方法中返回異常消息似乎是一個非常糟糕的主意。

您需要了解:嘗試/捕獲的原因是什么? 您想在那里實現什么? 如果您只想記錄該異常,則最好在某些頂級框架錯誤處理中執行此操作,而不是在此處進行。

如果要隱藏一些異常(通常這是一個壞主意,但是有一些異常),則可以在此處返回null來表示未發現任何異常。 而且,在您擁有可以返回的鍵的值之后,發生異常的可能性很小(因為您只是返回它,所以甚至不可能)。

還不是100%清楚您要轉發什么。 如果是異常本身,則可以執行以下操作:

try
{
}
catch (Exception exception)
{
    throw;
}

現在,這將正確地使用原始堆棧跟蹤重新拋出異常。 盡管此代碼本身沒有用,但是如果需要,您可以在throw之前添加一些處理。

嘗試,那就是嘗試,捕捉,最終的目的

Try
{
    String result = GetKey();
    if (result == null)
    {
        Console.WriteLine("null");
    }
    else
    {
        Console.WriteLine(result);
    }
}
catch
{
    Console.WriteLine("Reading key failed");
}
    Console.ReadKey();

另請注意,我從重復調用GetKey更改了您的代碼-這是不必要的

當代碼明確期望某些鍵值時返回異常消息是一個非常糟糕的主意。

通常的方法是將異常作為應用程序特定的類型重新拋出,而將原始異常作為內部異常。 因此,在方法的catch塊中,您可以執行以下操作:

 catch (Exception ex)
 {
     throw new MyApplicationException(ex);
 }

其中MyApplicationException是您自己的異常類型,然后您可以在主程序中使用

catch (MyApplicationException ex)
{
    ////Handle it!
}

您可以通過將其設置為TryGetKey而不是僅GetKey來處理異常,從而以更加實用的方式編寫GetKey。 像這樣

static bool TryGetKey(out string result)
{
    result = null;

    try
    {
        using (RegistryKey Adapter = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}", true))
        {
            foreach (string Keyname in Adapter.GetSubKeyNames())
            {
                RegistryKey Key = Adapter.OpenSubKey(Keyname);

                if (Key.GetValue("DriverDesc").ToString() != "somename")
                {
                    result = Key.GetValue("ComponentId").ToString();
                    return true;
                }
            }
        }
    }
    catch (Exception ex)
    {
        Trace.WriteLine(ex.Message);
    }

    return false;
}

並且在調用代碼中,您應該檢查該函數的結果是什么,以便對鍵進行操作或處理失敗。

var key = string.Empty;
if (TryGetKey(out key))
{
    // Got key can do something.
}
else
{
    // Something went wrong, what should the application do?
}

通常,將整個代碼塊包裝在try-catch中也被認為是不好的做法。

暫無
暫無

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

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