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");
        }
    }
  • 相关阅读:
    java加密算法-MD5
    java加密算法-DES
    java加密算法-AES
    java写入内容到本地文件 -读取文件内容
    java 图片base64互转
    java上传文件
    判断请求是否是同一个域名
    java计算两个经纬度之间的距离
    java请求url可以带参数
    Java编程基础篇第五章
  • 原文地址:https://www.cnblogs.com/PersonalDiary/p/12981766.html
Copyright © 2011-2022 走看看