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里面都要添加这些异常处理代码,造成代码冗余拉杂

SpringBoot学习12:springboot异常处理方式2(使用@ExceptionHandle注解)的更多相关文章

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

    1.编写controller package com.bjsxt.controller; import org.springframework.stereotype.Controller; impor ...

  2. SpringBoot学习(四)-->SpringBoot快速入门,开山篇

    Spring Boot简介 Spring Boot的目的在于创建和启动新的基于Spring框架的项目.Spring Boot会选择最适合的Spring子项目和第三方开源库进行整合.大部分Spring ...

  3. SpringBoot学习之SpringBoot执行器

    在以往的分布式开发当中,各个服务节点的监控必不可少.监控包含有很多方面,比如说:内存占用情况,节点是否健康等.在spring-boot会给我们提供相关资源监控叫做spring-boot-actuato ...

  4. SpringBoot学习(八)-->SpringBoot之过滤器、监听器

    本文将直接使用@WebFilter和@WebListener的方式,完成一个Filter 和一个 Listener. 过滤器(Filter)和 监听器(Listener)的注册方法和 Servlet ...

  5. SpringBoot学习(七)-->SpringBoot在web开发中的配置

    SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...

  6. SpringBoot学习(一):SpringBoot入门

    1.Spring Boot 简介 1) 简化Spring应用开发的一个框架: 2) 整个Spring技术栈的一个大整合: 3) J2EE开发的一站式解决方案: 2.微服务 2014,martin fo ...

  7. SpringBoot学习(六)-->SpringBoot的自动配置的原理

    Spring Boot的自动配置的原理 Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入 ...

  8. SpringBoot学习(五)-->SpringBoot的核心

    SpringBoot的核心 1.入口类和@SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的J ...

  9. SpringBoot学习<二>——SpringBoot的默认配置文件application和多环境配置

    一.SpringBoot的默认文件appliction 上一篇文章已经说明,springboot启动会内嵌tomcat,端口也是默认的8080,如果我们想要改变端口如果做呢? 在springboot项 ...

随机推荐

  1. python 自动安装工具 setuptools(easy_install) 的使用

    1.下载安装 python 安装工具,下载地址:http://pypi.python.org/pypi/setuptools ,可以找到正确的版本进行下载. 2.解压缩后双击 ez_setup.py ...

  2. 如何修改FlashFXP默认编辑工具使用记事本打开

    FlashFXP如果不设置默认编辑工具,那么当你打开html文档的时候,默认会用word打开,很不方便,其实简单的设置下,可以默认以任何工具打开.具体设置方法如下: 选项>>关联文件. 然 ...

  3. pat1017. Queueing at Bank (25)

    1017. Queueing at Bank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Supp ...

  4. Makefile2

    规范 target可以是Object file, 可执行文件或者标签(标签一般没有依赖) 越靠近最终结果的target卸载越前面 定义target前, 定义CC, SRC, CFLAGS, OBJS, ...

  5. BNU 27847——Cellphone Typing——————【字典树】

    Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Origi ...

  6. HDU 5424——Rikka with Graph II——————【哈密顿路径】

    Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  7. Calendar计算一个月前的日期,踩坑记录

    错误示范:calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);//获取一个月前的今天这种写法假设传入的日期为2019-03-3 ...

  8. IT相关术语、缩略词

    CLI Command Line Interface 命令行界面 GUI Graphical User Interface 图形用户界面 IP Internet Protocol 因特网协议 JDK ...

  9. 从零开始的全栈工程师——js篇2.20(事件对象 冒泡与捕获)

    一.复习 面向对象 1)单例模式 2)工厂模式 3)构造函数 ①类js天生自带的类 基类object function array number math boolean date regexp st ...

  10. LOJ#137. 最小瓶颈路 加强版(Kruskal重构树 rmq求LCA)

    题意 三倍经验哇咔咔 #137. 最小瓶颈路 加强版 #6021. 「from CommonAnts」寻找 LCR #136. 最小瓶颈路 Sol 首先可以证明,两点之间边权最大值最小的路径一定是在最 ...