I got a method that throw an exception if I try to insert an existing object in DB.
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);
}
So when the exception is thrown I wanna catch it and display an error messsage in my managed bean.
PS: In my managed bean I just call the method :
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();
}
}
Its bad practice to catch or throw
Exception. If any code you use throws a checked exception then just catch that specific exception, and try to minimize the size of yourtry-catchblocks.