zoukankan      html  css  js  c++  java
  • JAVA(一)--自定义异常

    1. BaseException

    package com.royal.summer.exception;
    
    import lombok.Data;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * <p>
     * BaseException
     * </p>
     *
     * @author 3hzpf
     * @since 2020-05-28
     */
    @Data
    public class BaseException extends RuntimeException {
    
        /** slf4j 日志*/
        public static final Logger LOGGER = LoggerFactory.getLogger(BaseException.class);
    
        public BaseException(){}
    
        public BaseException(String message) {
            super(message);
        }
    
        public BaseException(String message, Throwable cause) {
            super(message, cause);
        }
    
        public BaseException(Throwable cause) {
            super(cause);
        }
    
        protected BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
        }
    }

    2. CustomException (自定义异常一)

    package com.royal.summer.exception;
    
    /**
     * <p>
     * MyException
     * </p>
     *
     * @author zpf
     * @since 2020-05-28
     */
    public class CustomException extends BaseException {
    
        public CustomException() {}
    
        public CustomException(String message) {
            super(message);
        }
    
        public CustomException(String message, Throwable cause) {
            super(message, cause);
        }
    
        public CustomException(Throwable cause) {
            super(cause);
        }
    
        public CustomException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
        }
    }

    3. MyException(自定义异常二)

    package com.royal.summer.exception;
    
    /**
     * <p>
     * MyException
     * </p>
     *
     * @author 3hzpf
     * @since 2020-05-28
     */
    public class MyException extends BaseException {
    
        public MyException() {}
    
        public MyException(String message) {
            super(message);
        }
    
        public MyException(String message, Throwable cause) {
            super(message, cause);
        }
    
        public MyException(Throwable cause) {
            super(cause);
        }
    
        public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
        }
    }

    4. 全局异常处理

    package com.royal.summer.exception;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.RestControllerAdvice;
    
    /**
     * <p>
     *      GlobalExceptionHandler 全局自定义异常处理
     *      @RestControllerAdvice  对自定义异常进行拦截实现统一异常返回处理,此注解相当于(@ControllerAdvice + @ResponseBody)
     * </p>
     *
     * @author zpf
     * @since 2020-05-28
     */
    @RestControllerAdvice
    public class GlobalExceptionHandler {
    
        /** slf4j 日志*/
        public static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    
        @ExceptionHandler(MyException.class)
        public String handleMyException(MyException e){
            LOGGER.info("MyException:{}",e.getMessage());
            return "MyException";
        }
    
        @ExceptionHandler(CustomException.class)
        public String handleCustomException(CustomException e){
            LOGGER.info("CustomException:{}",e.getMessage());
            return "CustomException";
        }
    }

    5. 测试Controller

    package com.royal.summer.controller;
    
    import com.royal.summer.exception.CustomException;
    import com.royal.summer.exception.MyException;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * <p>
     * TestController
     * </p>
     *
     * @author zpf
     * @since 2020-05-28
     */
    @RestController
    public class TestController {
    
        @GetMapping("/test")
        public String test() throws Exception{
            throw new CustomException("异常CustomException");
        }
    
        @GetMapping("/test2")
        public String test2() throws Exception{
            throw new MyException("异常MyException");
        }
    }
  • 相关阅读:
    CRM更新行数量汇总的一些注意点
    [转]IT人从业方向
    地球撞击
    如何将Dynamic CRM Activities添加到VS工具箱
    linux本地 yum环境建立
    【转】根据条件修改GridView命令按钮显示的文字
    【转】Asp.net 2.0三层架构的构建与理解
    GridView的常用用法总结说明
    IE中的奇怪问题
    解决打不开 RSA 密钥容器 即:加密web.config中的内容
  • 原文地址:https://www.cnblogs.com/PersonalDiary/p/12981766.html
Copyright © 2011-2022 走看看