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");
    }
}

 

posted @ 2020-05-28 16:40  减字木兰花·回风落景  阅读(171)  评论(0编辑  收藏  举报