SpringBoot: 12.异常处理方式2(使用@ExceptionHandle注解)(转)

 

1、编写controller

复制代码
package com.bjsxt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Administrator on 2019/2/13.
 */
@Controller
public class IndexController {

    @RequestMapping("toIndex")
    public String toIndex(){
        String str=null;
        //会发生空指针异常
        str.length();
        return "index";
    }

    @RequestMapping("toIndex2")
    public String toIndex2(){
        //会发生算术异常
        int num=10/0;
        return "index";
    }

    @RequestMapping("toIndex3")
    public String toIndex3(){
        //会发生数组下标越界异常,该异常没有专门处理,会跳到自定义的错误页面
        String[] arr={"aa","bb","cc"};
        System.out.print(arr[3]);
        return "index";
    }

    /**
     * 处理ArithmeticException异常,该@ExceptionHandler注释的value属性可以是一个数组,
     * 然后再根据注入的exception判断对不同异常分别进行不同的处理,也可以写多个controller,
     * 对多个不同异常进行处理,这里采用第二种
     * @param e 会将产生异常对象注入到方法中
     * @return 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视图的指定
     */
    @ExceptionHandler(value = {java.lang.ArithmeticException.class})
    public ModelAndView arithmeticExceptionHandler(Exception e){
        ModelAndView mv=new ModelAndView("error_arithmetic");
        mv.addObject("msg",e.toString());
        return mv;
    }


    /**
     * 处理NullPointerException异常
     * @param e 会将产生异常对象注入到方法中
     * @return 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视图的指定
     */
    @ExceptionHandler(value = {java.lang.NullPointerException.class})
    public ModelAndView nullPointerExceptionHandler(Exception e){
        ModelAndView mv=new ModelAndView("error_nullPointer");
        mv.addObject("msg",e.toString());
        return mv;
    }

}
复制代码

目录结构,该方法存在的弊端就是在每一个controller里面都要添加这些异常处理代码,造成代码冗余拉杂

1、编写controller

复制代码
package com.bjsxt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Administrator on 2019/2/13.
 */
@Controller
public class IndexController {

    @RequestMapping("toIndex")
    public String toIndex(){
        String str=null;
        //会发生空指针异常
        str.length();
        return "index";
    }

    @RequestMapping("toIndex2")
    public String toIndex2(){
        //会发生算术异常
        int num=10/0;
        return "index";
    }

    @RequestMapping("toIndex3")
    public String toIndex3(){
        //会发生数组下标越界异常,该异常没有专门处理,会跳到自定义的错误页面
        String[] arr={"aa","bb","cc"};
        System.out.print(arr[3]);
        return "index";
    }

    /**
     * 处理ArithmeticException异常,该@ExceptionHandler注释的value属性可以是一个数组,
     * 然后再根据注入的exception判断对不同异常分别进行不同的处理,也可以写多个controller,
     * 对多个不同异常进行处理,这里采用第二种
     * @param e 会将产生异常对象注入到方法中
     * @return 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视图的指定
     */
    @ExceptionHandler(value = {java.lang.ArithmeticException.class})
    public ModelAndView arithmeticExceptionHandler(Exception e){
        ModelAndView mv=new ModelAndView("error_arithmetic");
        mv.addObject("msg",e.toString());
        return mv;
    }


    /**
     * 处理NullPointerException异常
     * @param e 会将产生异常对象注入到方法中
     * @return 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视图的指定
     */
    @ExceptionHandler(value = {java.lang.NullPointerException.class})
    public ModelAndView nullPointerExceptionHandler(Exception e){
        ModelAndView mv=new ModelAndView("error_nullPointer");
        mv.addObject("msg",e.toString());
        return mv;
    }

}
复制代码

目录结构,该方法存在的弊端就是在每一个controller里面都要添加这些异常处理代码,造成代码冗余拉杂

猜你喜欢

转载自www.cnblogs.com/kuangzhisen/p/10427178.html