如果我尝试在数据库中插入现有对象,我得到了一个引发异常的方法。如何在调用类中捕获抛出的异常
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