在下面的兩個示例代碼中,我試圖通過使用C#常規方法和對象初始值設定項實例化名爲的測試類。無法識別在C#中使用對象初始值設定項時引發異常的屬性
DateTime? nullDate = null; //this value will come from somewhere else
DateTime? notNullDate = DateTime.Now;
var test = new Test();
test.Date = nullDate.Value; //exception will throw here
test.Name = "String";
test.AnotherDate = notNullDate.Value;
在上面的示例代碼中,我可以清楚地瞭解哪些屬性在調試時顯示異常。
DateTime? nullDate = null; //this value will come from somewhere else
DateTime? notNullDate = DateTime.Now;
var test = new Test
{
Date = nullDate.Value,
Name = "String",
AnotherDate = notNullDate.Value
};
在上面的代碼中,當我使用對象初始值設定項時,我無法理解哪個屬性被拋出異常。在這裏,我無法逐行調試。如果我初始化了很多屬性,則很難識別。
這是我的問題:如何識別哪個屬性顯示異常窗口的異常?現在內部的異常是空的。
你不能 - 這就是爲什麼你應該在調用初始化程序之前做空檢查等。異常不會給你任何關於_what_的上下文是空 –
@DStanley我可以檢查空引用,但我的問題是沒有其他方法來確定哪個屬性拋出異常? – JEMI
否 - 該例外不會導致_what_爲空。 –