使用自定义error页面并没有真正处理异常,可以使用@ExceptionHandler注解处理异常。  
    如果在Controller中有一个使用@ExceptionHandler注解修饰的方法,那么当Controller的任何方法抛出异常时,都由该方法处理异常。
使用@ExceptionHandler注解处理异常。
1.在控制器类中添加使用@ExceptionHandler注解修饰的方法
2.创建sqlError、myError和noError页面
3.运行
在控制器类中添加使用@ExceptionHandler注解修饰的方法

@ExceptionHandler(value=Exception.class)
public String handlerException(Exception e) {
    //数据库异常
    if (e instanceof SQLException) {
        return "sqlError";
    } else if (e instanceof MyException) {//自定义异常
        return "myError";
    } else {//未知异常
        return "error";
    }
    }
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.error</groupId>
    <artifactId>SpringBoot-Error</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <!-- 声明项目配置依赖编码格式为 utf-8 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <fastjson.version>1.2.24</fastjson.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <!-- 由于commons-fileupload组件不属于Spring Boot,所以需要加上版本 -->
            <version>1.3.3</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
server.servlet.context-path=/ch5_3
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>error</title>
<link rel="stylesheet" th:href="@{css/bootstrap.min.css}" />
<!-- 默认访问 src/main/resources/static下的css文件夹-->
<link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}" />
</head>
<body>
  <div class="panel-l container clearfix">
        <div class="error">
            <p class="title"><span class="code" th:text="${status}"></span>非常抱歉,没有找到您要查看的页面</p>
            <div class="common-hint-word">
                <div th:text="${#dates.format(timestamp,'yyyy-MM-dd HH:mm:ss')}"></div>
                <div th:text="${message}"></div>
                <div th:text="${error}"></div>
            </div>
        </div>
    </div>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>index</title>
<link rel="stylesheet" th:href="@{css/bootstrap.min.css}" />
<!-- 默认访问 src/main/resources/static下的css文件夹-->
<link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}" />
</head>
<body>
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">异常处理示例</h3>
        </div>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-md-4 col-sm-6">
                <a th:href="@{db}">处理数据库异常</a><br>
                <a th:href="@{my}">处理自定义异常</a><br>
                <a th:href="@{no}">处理未知错误</a>
                <hr>
                <a th:href="@{nofound}">404错误</a>
            </div>
        </div>
    </div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>myError</title>
</head>
<body>
    自定义异常。
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    未知异常。
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>sql</title>
</head>
<body>
    SQL异常。
</body>
</html>
package com.ch.ch5_3.controller;

import java.sql.SQLException;

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

import com.ch.ch5_3.exception.MyException;

public class BaseController {
    @ExceptionHandler(value = Exception.class)
    public String handlerException(Exception e) {
        // 数据库异常
        if (e instanceof SQLException) {
            return "sqlError";
        } else if (e instanceof MyException) {// 自定义异常
            return "myError";
        } else {// 未知异常
            return "noError";
        }
    }
}
package com.ch.ch5_3.controller;

import java.sql.SQLException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.ch.ch5_3.exception.MyException;

@ControllerAdvice
public class GlobalExceptionHandlerController {
    @ExceptionHandler(value = Exception.class)
    public String handlerException(Exception e) {
        // 数据库异常
        if (e instanceof SQLException) {
            return "sqlError";
        } else if (e instanceof MyException) {// 自定义异常
            return "myError";
        } else {// 未知异常
            return "noError";
        }
    }
}

 

 

 

 

@ControllerAdvice注解

    ControllerAdvice注解,顾名思义,是一个增强的Controller。使用该Controller,可以实现三个方面的功能:全局异常处理、全局数据绑定以及全局数据预处理。

使用@ControllerAdvice注解的类是当前Spring Boot应用中所有类的统一异常处理类,该类中使用@ExceptionHandler注解的方法用来统一处理异常,不需要在每个Controller中逐一定义异常处理方法,这是因为对所有注解了@RequestMapping的控制器方法有效。