如果我嘗試在數據庫中插入現有對象,我得到了一個引發異常的方法。如何在調用類中捕獲拋出的異常
public void addInDB() throws Exception {
if (isInBase()){
throw new Exception ("[ReqFamily->addInDB] requirment already in base");
}
int idParent = m_parent.getIdBdd();
idBdd = pSQLRequirement.add(name, description, 0, idParent,
ReqPlugin.getProjectRef().getIdBdd(), 100);
}
因此,當發生異常時,我想抓住它,在我管理的bean顯示錯誤訊息話題。
PS:在我管理的Bean只需要調用方法:
void addReq(Requirement req){
try {
ReqFamily pReqParent = (ReqFamily) selectedNode.getData();
req.setParent(pReqParent);
req.addInDB();//here i want to catch it
DefaultTreeNode newReqNode = new DefaultTreeNode(req,selectedNode);
if (pReqParent!=null){
pReqParent.addRequirement(req);
}
} catch (Exception ex){
ex.printStackTrace();
}
}
你的意思是抓有例外和日誌它並繼續進一步處理? – kosa
我的意思是防止拋出異常,而是向用戶顯示錯誤信息:該對象已經存在於數據庫中 – AmiraGL