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         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM