我有兩個構造函數用於我的類,一個需要File
對象,另一個需要String
對象,我想使用this
關鍵字。具有實現的功能是File
作爲參數的函數,具有String
的函數將調用this
。現在我想檢查構造函數中的異常,其中需要String
但我得到錯誤,應該是第一行this
。如何檢查錯誤,然後致電this
。構造函數處理異常並使用此關鍵字Java
這裏是我的代碼:
public Test (String filename) {
if (filename == null)
throw new NullPointerException("The String you entered is null.");
if (filename.isEmpty())
throw new IllegalArgumentException("The String you entered is empty.");
this(new File(filename)); // error
}
public Test (File f) {
/* implementation here */
}
這是確切的錯誤:Constructor call must be the first statement in a constructor
你得到什麼錯誤? – Thomas 2013-04-20 14:24:38
發佈的代碼與您所描述的相反! – NINCOMPOOP 2013-04-20 14:24:40