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");
        }
    }
  • 相关阅读:
    计算机科学与软件工程的区别
    中文编程对中国程序员是一个“银弹”吗?
    CocoaPods的使用心得
    初学 Swift (实现加减乘除功能和函数的基本类型)
    error itms-90096?苹果提交二进制文件时,报这个错(解决方案)
    因为年轻,所以拼搏
    [转载]C#中的interface abstract和virtual
    一个简单的.NET MVC实例
    Unity3d + Jenkins自动构建IOS篇遇到的问题。
    BZOJ1005: [HNOI2008]明明的烦恼
  • 原文地址:https://www.cnblogs.com/PersonalDiary/p/12981766.html
Copyright © 2011-2022 走看看