spring的异常处理

Posted 天际星痕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring的异常处理相关的知识,希望对你有一定的参考价值。

出自于:https://blog.csdn.net/he90227/article/details/46309297   ---- 利用Spring进行统一异常处理的两种方式。

 

原文:https://blog.csdn.net/he90227/article/details/46309297?utm_source=copy

1.实现接口HandlerExceptionResolver

package com.jay.platform.exception.handler;
 
import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.Date;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
 
import com.jay.platform.exception.AjaxException;
import com.jay.platform.exception.BusinessException;
 
/**
 * 
 * @ClassName: ExceptionHandler
 * @Description: 统一异常处理 , 注意这里可以处理Controller层抛出的异常,但不处理Controller捕获的异常(Controller捕获的异常,这里不再处理)
 * @author Jay He
 * @date 2014年11月4日 上午10:22:11
 * 
 */
public class ExceptionHandler implements HandlerExceptionResolver {
    private static final Logger LOGGER = Logger
	    .getLogger(ExceptionHandler.class);
 
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
	    HttpServletResponse response, Object handler, Exception ex) {
	LOGGER.error(new Date().toLocaleString() + "异常信息", ex);
	if (ex instanceof NumberFormatException) {
	    return new ModelAndView("exception/number");
	} else if (ex instanceof NullPointerException) {
	    return new ModelAndView("exception/null");
	} else if (ex instanceof BusinessException) {
	    return new ModelAndView("exception/business");
	} else if (ex instanceof SocketTimeoutException
		|| ex instanceof ConnectException) {
	    try {
		response.getWriter().write("网络异常");
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	    return new ModelAndView("exception/net_error");
	}else if(ex instanceof AjaxException){
	   System.out.println("-=-=");
	}
	return new ModelAndView("exception/exception");
    }
 
}

  

2.继承AbstractHandlerExceptionResolver类

package com.jay.platform.exception.handler;
 
import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.Date;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
 
import com.jay.platform.exception.AjaxException;
import com.jay.platform.exception.BusinessException;
 
/**
 * 
 * @ClassName: ExceptionHandler
 * @Description: 统一异常处理 , 注意这里可以处理Controller层抛出的异常,但不处理Controller捕获的异常(Controller捕获的异常,这里不再处理)
 * @author Jay He
 * @date 2014年11月4日 上午10:22:11
 * 
 */
public class ExceptionHandler extends AbstractHandlerExceptionResolver{
    private static final Logger LOGGER = Logger
	    .getLogger(ExceptionHandler.class);
 
    @Override
    protected ModelAndView doResolveException(HttpServletRequest request,
	    HttpServletResponse response, Object handler, Exception ex) {
	<pre name="code" class="java">LOGGER.error(new Date().toLocaleString() + "异常信息", ex);
	if (ex instanceof NumberFormatException) {
	    return new ModelAndView("exception/number");
	} else if (ex instanceof NullPointerException) {
	    return new ModelAndView("exception/null");
	} else if (ex instanceof BusinessException) {
	    return new ModelAndView("exception/business");
	} else if (ex instanceof SocketTimeoutException
		|| ex instanceof ConnectException) {
	    try {
		response.getWriter().write("网络异常");
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	    return new ModelAndView("exception/net_error");
	}else if(ex instanceof AjaxException){
	   System.out.println("-=-=");
	}
	return new ModelAndView("exception/exception");

在配置文件中添加自定义异常处理模块

 <!-- Spring MVC的统一异常处理 -->
    <bean id="exceptionResolver" class="com.jay.platform.exception.handler.ExceptionHandler"/>  
错误页面展示:例:
 
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error页面</title>
</head>
<body>
<center>
<h1>Error页面</h1><br>
<h2>出错信息</h2>
<br><br>
错误描述
<br>
${errorTips }
<br><br>
<br><br>
错误异常信息栈
<br><br>
${ex }
</center>
</body>
</html>

  

以上是关于spring的异常处理的主要内容,如果未能解决你的问题,请参考以下文章

使用片段中的处理程序时出现非法状态异常

PCL异常处理:pcl 1.8.13rdpartyoostincludeoost-1_64oost ypeofmsvc ypeof_impl.hpp(125): error(代码片段

如何使用 Spring 异常处理 POST REST 更正错误状态代码

Spring统一异常处理

Java异常处理机制

javaweb+spring 项目集成异常的处理