我在mClass構造如下代碼:處理空值,其中不允許
public mClass(Context ctx) {
super();
this.ctx = ctx;
}
上下文不能爲空,因爲它necesary爲對象的操作。所以如果我允許創建一個新的mClass(null),它將在稍後中斷。
當創建對象時,我想崩潰,因爲發生錯誤情況時。這是什麼標準的做法?
例如使
public mClass(Context ctx) {
super();
if(ctx==null) throw new Exception ("....");
this.ctx = ctx;
}
軍隊申報方法例外運動員,我不wan't這樣做,因爲傳遞一個空值是不常見