在Delphi中,如果在構造對象期間發生異常:任何分配的內存將被釋放並引發異常。例如,下面是保證要麼返回一個有效的Camera
對象,或者拋出一個異常:CLR:如果構造函數失敗,它總是會拋出異常?
Camera c = new Camera();
你從未必須檢查所生成的變量爲空:
Camera c = new Camera();
if (c == null)
throw new Exception("Error constructing Camera") //waste of time
在CLR中也是如此嗎?
還有其他的合成結構,其中返回值保證有效,或拋出異常?
- 創建結構(例如Rectangle)?
- 獲取枚舉的成員?
- Object.ToString()的結果?
- mathmatical operations?
在執行數學的情況下:
Int32 aspect = 1650.0/1080.0;
if (aspect == null)
throw new Exception("Division of two numbers returned null")