我正在進行一些驗證。有一些必填字段,其中一些是可選的。對於必填字段,我拋出異常,但對於可選字段,我必須打印警告,並且必須在我的方法中繼續。我沒有辦法做警告部分。有人可以幫忙嗎?拋出異常並允許繼續進行方法
public void method(String param1, String param2){
if(param1 == null){
throw new IllegalArgumentException("mandatory field");
}
//Here for param2, I want to throw eception, but want to proceed further to next line.
//Execute my code here
}
'try {...} catch(){...} fincally {...}' –
您只需要嘗試{} catch {}。看到[這](http://stackoverflow.com/questions/9329568/how-to-continue-executing-a-java-program-after-an-exception-is- thrown),似乎很相似。 – gvlachakis
如果你想繼續,你不能拋出異常。只需發送警告以字符串的形式輸出,在代碼中使用一些函數即可。 – Jadamec