【统一异常处理】可直接cv

package com.example.chiptest.exception;

import com.example.chiptest.Util.ResponseInformation;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.InvalidClassException;
import java.sql.SQLDataException;
import java.sql.SQLException;

/**
 * @author luzey
 * 统一异常处理
 */
@RestControllerAdvice	//捕获各种异常 ,用文字描述
@ResponseBody
public class ExceptionControllerAdvice {
// @ExceptionHandler(value = 要处理的异常字节码)
    @ExceptionHandler(value = IncorrectCredentialsException.class)
    public ResponseInformation incorrectCredentialsException(IncorrectCredentialsException incorrectCredentialsException) {
        System.err.println(incorrectCredentialsException.getMessage());
        for (StackTraceElement stackTraceElement : incorrectCredentialsException.getStackTrace()) {
            System.err.println(stackTraceElement);
        }
        return ResponseInformation.ERROR("用户名或密码错误");
    }

    @ExceptionHandler(value = IndexOutOfBoundsException.class)
    public ResponseInformation indexOutOfBoundsException(IndexOutOfBoundsException indexOutOfBoundsException) {
        System.err.println(indexOutOfBoundsException.getMessage());
        for (StackTraceElement stackTraceElement : indexOutOfBoundsException.getStackTrace()) {
            System.err.println(stackTraceElement);
        }
        return ResponseInformation.ERROR("数组角标越界");
    }

    @ExceptionHandler(value = NullPointerException.class)
    public ResponseInformation nullPointerException(NullPointerException nullPointerException) {
        System.err.println(nullPointerException.getMessage());
        for (StackTraceElement stackTraceElement : nullPointerException.getStackTrace()) {
            System.out.println("****************************************************************");
            System.err.println(stackTraceElement);
            System.out.println("****************************************************************");

        }
        return ResponseInformation.ERROR("空指针异常");
    }

    @ExceptionHandler(value = InvalidClassException.class)
    public ResponseInformation invalidClassException(InvalidClassException invalidClassException) {
        System.err.println(invalidClassException.getMessage());
        for (StackTraceElement stackTraceElement : invalidClassException.getStackTrace()) {
            System.err.println(stackTraceElement);
        }
        return ResponseInformation.ERROR("更改了可序列化的文件(例:BaseEntity),缓存中还在里面,clean+install即可解决问题");
    }

    @ExceptionHandler(value = SQLException.class)
    public ResponseInformation invalidClassException(SQLException SQLException) {
        System.err.println(SQLException.getMessage());
        for (StackTraceElement stackTraceElement : SQLException.getStackTrace()) {
            System.err.println(stackTraceElement);
        }
        return ResponseInformation.ERROR("SQL语句出现异常");
    }

    @ExceptionHandler(value = SQLDataException.class)
    public ResponseInformation invalidClassException(SQLDataException sqlDataException) {
        System.err.println(sqlDataException.getMessage());
        for (StackTraceElement stackTraceElement : sqlDataException.getStackTrace()) {
            System.err.println(stackTraceElement);
        }
        return ResponseInformation.ERROR("无法从某个字符串中确定类型");
    }

    @ExceptionHandler(value = Exception.class)
    public ResponseInformation exception(Exception exception) {
        System.err.println(exception.getMessage());
        for (StackTraceElement stackTraceElement : exception.getStackTrace()) {
            System.err.println(stackTraceElement);
        }
        return ResponseInformation.ERROR("系统繁忙");

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值