目录

引言

统一异常处理

异常全部监测


引言

统一异常处理


实例理解

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/test")
    public int test() {
        Object obj = null;
        obj.hashCode();
        return 1;
    }
}

package com.example.demo.component;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@ControllerAdvice
@ResponseBody
public class MyExceptionAdvice {

    @ExceptionHandler(NullPointerException.class)
    public HashMap<String,Object&gt; doNullPointerException(NullPointerException e) {
//        自定义异常处理
        HashMap<String,Object&gt; result = new HashMap<&gt;();
        result.put("code",-1);
        result.put("msg","异常类型:" + e);
        result.put("data",null);
        return result;
    }
}

异常全部监测


实例理解

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/arr")
    public int arr() {
        int[] array = new int[3];
        for (int i = 0; i < 4; i++) {
            array[i] = i;
        }
        return 1;
    }
}
package com.example.demo.component;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@ControllerAdvice
@ResponseBody
public class MyExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public HashMap<String,Object> doException(Exception e) {
//        自定义异常处理
        HashMap<String,Object> result = new HashMap<>();
        result.put("code",-2);
        result.put("msg","异常类型:" + e);
        result.put("data",null);
        return result;
    }
}


实例理解二

  • 此处我们对 Exception ,即所有异常的父类 进行了异常统一处理
  • 此外我们还单独对 NullPointException ,即空指针异常 进行了异常统一处理
package com.example.demo.component;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@ControllerAdvice
@ResponseBody
public class MyExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public HashMap<String,Object> doException(Exception e) {
//        自定义异常处理
        HashMap<String,Object> result = new HashMap<>();
        result.put("code",-2);
        result.put("msg","异常类型:" + e);
        result.put("data",null);
        return result;
    }

//    对 空指针异常进行特殊处理
    @ExceptionHandler(NullPointerException.class)
    public HashMap<String,Object> doNullPointerException(NullPointerException e) {
//        自定义异常处理
        HashMap<String,Object> result = new HashMap<>();
        result.put("code",-1);
        result.put("msg","异常类型:" + e);
        result.put("data",null);
        return result;
    }
}

  • 由上图可知,当 父类和子类异常 同时存在的情况下,优先子类自己的异常处理
  • 从而可以实现 对所有异常进行统一处理,对个别有要求的异常进行单独的特殊处理

原文地址:https://blog.csdn.net/weixin_63888301/article/details/134796502

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_47318.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注