日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java自定义异常并处理异常

發(fā)布時間:2024/3/24 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java自定义异常并处理异常 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

自定義異常:

/*** 自定義異常** @author ruoyi*/ @Data public final class MyException extends RuntimeException {private Integer status = ExceptionCode.SERVICEEXCEPTION.getCode();public MyException() {}public MyException(ExceptionCode status,String message) {super(status.getCode()+"&"+message);this.status = status.getCode();}public MyException(Throwable cause) {super(cause);}public MyException(String message, Throwable cause) {super(message, cause);}public MyException(String message, Throwable cause,boolean enableSuppression, boolean writableStackTrace) {super(message, cause, enableSuppression, writableStackTrace);}} /*** 自定義異常狀態(tài)* * @author ruoyi*/ public enum ExceptionCode {TOKENEXCEPTION(40000),//登錄TOKEN異常SERVICEEXCEPTION(41000),//業(yè)務(wù)異常(包括參數(shù))AUTHEXCEPTION(42000);//權(quán)限異常private final Integer code;ExceptionCode(Integer code){this.code = code;}public Integer getCode(){return code;}}

自定義異常處理:

/*** 自定義異常處理*/ @ControllerAdvice @Slf4j public class MyExceptionHandler {@org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)public void handleException(HttpServletRequest request, HttpServletResponse response, Exception ex) {//返回給前端的信息Map<String, Object> map = new HashMap<String, Object>();String message = ex.getMessage();Integer status = 500;if(ex.getClass().equals(MyException.class)){//是自定義異常String[] str = message.split("&");status = MyUtil.convertToInt(str[0], 500);message = str[1];}else{message = "系統(tǒng)錯誤";}if (status.equals(500)) {System.out.println("非業(yè)務(wù)異常,請查看錯誤日志");log.error(MyExceptionUtil.getExceptionAllinformation(ex));} else {System.out.println("錯誤" + status + ":" + message);}map.put("status", status);map.put("message", message);request.setAttribute("errorMap", map);response.setHeader("Content-Type", "application/json");response.setStatus(403);MyUtil.writeObjectToClient(map, response);} } /*** 異步傳輸Json數(shù)據(jù)** @param obj* @param response* @throws JsonGenerationException* @throws JsonMappingException* @throws IOException* @author wzd* @description* @date 2013-2-28*/public static void writeObjectToClient(Object obj, HttpServletResponse response) {String jsonStr = JSON.toJSONString(obj);response.setCharacterEncoding("UTF-8");response.setContentType("text/html;charset=UTF-8");try {response.getWriter().print(jsonStr);} catch (IOException e) {e.printStackTrace();}}/*** 獲得異常的字符串形式信息* @param e 要打印的異常* @return*/public static String getExceptionAllinformation(Exception e) {ByteArrayOutputStream out = new ByteArrayOutputStream();PrintStream pout = new PrintStream(out);e.printStackTrace(pout);String ret = new String(out.toByteArray());pout.close();try {out.close();} catch (Exception e1) {e1.printStackTrace();}return ret;}

結(jié)束:

這樣只要

throw new MyException(ExceptionCode.SERVICEEXCEPTION,"");

就能返回異常信息給前端了,前端就能統(tǒng)一進(jìn)行異常處理了。

總結(jié)

以上是生活随笔為你收集整理的java自定义异常并处理异常的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。