异常处理思想:
当程序发生异常时,可以有2种方式处理(实际项目中2种方式一起使用,必要的时候,需要手动捕获异常来处理):
1.交给spring框架处理(例如后台发生错误,框架帮你统一返回500页面等,默认的操作都一样)
2.手动处理try--catch (例如当后台程序发生错误的时候,假设我偏要返回404页面,不是用框架返回)
下面针对2种方式做下 详细描述:
一。框架捕获异常
步骤一:自定义spring框架处理异常的类,Controller,用来统一捕获处理异常
package lms.common.web;
import lms.common.utils.Exceptions;
import lms.common.web.entity.ExceptionResponse;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;
@ControllerAdvice
public class DefaultExceptionHandler {
@ExceptionHandler({Exception.class})
public ModelAndView myException(Exception e){
System.out.println("myException=====");
Exceptions.setFlagEx(e);
ModelAndView mv = new ModelAndView();
if(e instanceof java.lang.NullPointerException){
String tips = Exceptions.getTips("NullPointerException",e);
errorMessageToConsoleAndFront(mv,tips,"error/500");
}else if(e instanceof java.sql.SQLException){
String tips = Exceptions.getTips("java.sql.SQLException",e);
errorMessageToConsoleAndFront(mv,tips,"error/500");
}else {
String tips = Exceptions.getTips("服务器尚未定义这种异常",e);
errorMessageToConsoleAndFront(mv,tips,"error/500");
}
return mv;
}
/**
* 把错误信息在控制台输出和在前台页面得到
*/
private ModelAndView errorMessageToConsoleAndFront(ModelAndView mv,String tips,String pageName){
//控制台输出
System.out.println(tips);
//传给前台,返回的页面
mv.addObject("errorMessage", tips);
/*mv.setViewName(pageName);*/
return mv;
}
}
package lms.modules.knowledge.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/test")
public class Test {
@RequestMapping("/testException")
public String testException(HttpServletRequest request,HttpServletResponse response){
String id = request.getParameter("id");
String name = request.getParameter("name");
System.out.println("id:" + id + "name : " + name);
throw new NullPointerException("===空执针异常测试====");//产生异常,不管,交由DefaultExceptionHandler处理
}
}
二.手动处理异常
测试类:
package lms.modules.knowledge.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/test")
public class Test {
@RequestMapping("/testException")
public String testException(HttpServletRequest request,HttpServletResponse response){
try{
throw new NullPointerException("===空执针异常测试===="); //发生异常
}catch(Exception e){ //捕获手动处理异常
/**
* 1.处理
* 2.记录日志
*/
}
return "";
}
}