Java 中 手动抛出异常: throw new Exception("错误信息") 错误信息的获得

时间:2023-03-09 19:39:44
Java 中 手动抛出异常: throw new Exception("错误信息") 错误信息的获得

当然需要先用try catch捕获,但注意new Exception("")括号里的字符串其实是异常原因,所以获取是要用ex.getCause().getMessage()

 int dylist = dyDao.findByHql("from ZcZcsqdy where yxbz = 1 and zcsqId = "+sqId, null).size();
if(dylist <= 0){
// logger.error("未添加任何资产//////////");
// return 0;
throw new Exception("未添加资产,请重新添加");
} //捕获异常 try{
processService.doNextFlow(getRequest());
}catch(Exception ex){
logger.error("error:" + ex.getMessage());
ex.printStackTrace();
setJsonString("{success:false,info:\""+ ex.getCause().getMessage()+"\"}");
}