Java 中 手动抛出异常: throw new Exception("错误信息") 错误信息的获得
当然需要先用try catch捕获,但注意new Exception("")括号里的字符串其实是异常原因,所以获取是要用ex.getCause().getMessage()
1 int dylist = dyDao.findByHql("from ZcZcsqdy where yxbz = 1 and zcsqId = "+sqId, null).size(); 2 if(dylist <= 0){ 3 // logger.error("未添加任何资产//////////"); 4 // return 0; 5 throw new Exception("未添加资产,请重新添加"); 6 } 7 8 9 10 11 12 //捕获异常 13 14 15 try{ 16 processService.doNextFlow(getRequest()); 17 }catch(Exception ex){ 18 logger.error("error:" + ex.getMessage()); 19 ex.printStackTrace(); 20 setJsonString("{success:false,info:\""+ ex.getCause().getMessage()+"\"}"); 21 }