簡體   English   中英

Try-Catch與If-Else | 我應該在這種情況下爭取使用If-Else還是只使用Try-Catch?

[英]Try-Catch vs If-Else | Should I fight to use If-Else in this or just go with the Try-Catch?

摘要


我已經接到了設置管理軟件的任務(對於小型美術師來說,他們的硬件肯定可以應付),但是,在將其提供給他們之前,我希望使其盡可能高效。 完成了主要功能,現在主要是進行修飾和優化。


        DateTime DueDate;
        try
        {
            DateTime.TryParse(dteCommission.SelectedDate.Value.Date.ToShortDateString(),
            out DueDate);
        }
        catch(Exception E)
        {
            MessageBox.Show("Due Date wasn't set. Defaulting to current date.", "Alert",
                MessageBoxButton.OK, MessageBoxImage.Warning);
            DueDate = DateTime.Parse(DateTime.Now.ToShortDateString());
        }

注意: Exception e僅用於快速完成它,真正的異常是已知的。 給出的錯誤是“可為空的對象必須具有值”。 System.InvalidOperationException


是最好在我正在處理時解決此問題,還是If-Else會更好? 如果是這樣,我將如何實施呢?

由於您已經在使用TryParse因此無需使用try ...catch塊。 不僅效率低下,而且也不干凈。 只需獲取DateTime.TryParse的返回值並做出決定即可。

var isDate = DateTime.TryParse(dteCommission.SelectedDate.Value.Date.ToShortDateString(),

然后, if (isDate){...} else {...}

異常e僅用於快速完成它,真正的異常是已知的。 給出的錯誤是“可為空的對象必須具有值”。 System.InvalidOperationException

您怎么知道在運行時會有不同的例外? 可以說NullReferenceException(例如)。 請記住,所有異常都實現Exception對象。

是最好在我正在處理時解決此問題,還是If-Else會更好?

您需要更好地處理錯誤。 您知道它可能為Nullable,因此您需要在繼續操作之前檢查它是否有價值。 您應該注意警告並優雅地處理它們。

如果是這樣,我將如何實施呢?

try
{
    if(dteCommission.SelectedDate.HasValue) 
    { 
        DateTime.TryParse(dteCommission.SelectedDate.Value.Date.ToShortDateString(),
                    out DueDate); 
    } else{
        MessageBox.Show("Due Date wasn't set. Defaulting to current date.", "Alert",
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                DueDate = DateTime.Parse(DateTime.Now.ToShortDateString());
    }
} 
catch(Exception e)
{
    Log.LogError(e);
    MessageBox.Show("Unhandle error occurred please call Admin", "Alert",
                    MessageBoxButton.OK, MessageBoxImage.Warning);
}

如果您承諾使用tryparse那么使用If-Else是更好的方法,它取決於tryparse方法的輸出。 但是,如果您使用的是Parse則可能會遇到以下異常之一:

  • ArgumentNullException(如果參數值為null)
  • FormatException(如果參數值不是整數值或格式不正確)
  • FormatException(如果參數值超出整數范圍)

因此最好使用異常處理。

對於第一種方法:

var isParsable = DateTime.TryParse(dteCommission.SelectedDate.Value.Date.ToShortDateString(),
out DueDate);
if (isParsable)
{
     //Continue With your Procedure
}
else
{
     MessageBox.Show("Due Date wasn't set. Defaulting to current date.", "Alert",
     MessageBoxButton.OK, MessageBoxImage.Warning);
}

對於第二種情況,您可以使用:

DateTime DueDate;
try
{
     var DueDate = DateTime.TryParse(dteCommission.SelectedDate.Value.ToString());

}
catch (Exception E)
{
     MessageBox.Show("Due Date wasn't set. Defaulting to current date.", "Alert",
     MessageBoxButton.OK, MessageBoxImage.Warning);
     //also you can you the exception type to make it clear for use if it is
     // an exception of Null, Format or Argument
}

我想建議在這種情況下使用if else語句,而不要使用例外情況,它也會被優化,並為您提供有意義地提供特定於該情況的消息的機會。

異常處理應僅用於處理未知方案。

暫無
暫無

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

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