java中异常自定义处理器_springmvc的自定义异常处理

一、自定义异常类:对于不同的异常类型定义异常类,继承Exception

CustomException.java

package com.sky.ssm.exception;

/**

* 系统自定义异常类

* 针对预期的异常,需要在程序中抛出此类的异常

* @author sk

*

*/

public class CustomException extends Exception{

//异常信息

public String message;

public CustomException(String message){

super(message);

this.message = message;

}

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

}

二、设置全局的异常处理

思路:当系统遇到异常,在程序中手动抛出,dao抛给service、service抛给controller,controller抛给前端控制器,前端控制器调用全局异常处理器。

全局异常处理器处理思路:

解析出异常类型

如果该异常类型是系统自定义的异常,直接取出异常信息,在错误页面展示

如果该异常类型不是系统自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)

Springmvc提供了一个HandlerExceptionResolver接口,只要实现这个接口,springmvc就会默认为这是一个全局的异常处理。

一个系统中,只能由一个全局的异常处理,

CustomExceptionResolver

package com.sky.ssm.exception;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;

import org.springframework.web.servlet.ModelAndView;

/**

* 全局异常处理器

* @author sk

*

*/

public class CustomExceptionResolver implements HandlerExceptionResolver{

/**

* 系统抛出的异常

*/

@Override

public ModelAndView resolveException(HttpServletRequest request,

HttpServletResponse response, Object handler, Exception ex) {

// handler就是处理器适配器要执行Handler对象(只有一个method)

//解析出异常类型

//如果该异常类型是系统自定义的异常,直接取出异常信息,在错误页面展示

//String message = null;

//if(ex instanceof CustomException){

//message = ((CustomException)ex).getMessage();

//}else{//如果该异常类型不是系统自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)

//message = "未知错误";

//}

//上边的代码变为

CustomException customException = null;

if(ex instanceof CustomException){

customException = (CustomException) ex;

}else{//如果该异常类型不是系统自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)

customException = new CustomException("未知错误");

}

//错误信息

String message = customException.getMessage();

//定义modelAndView

ModelAndView modelAndView = new ModelAndView();

//将错误信息传到页面

modelAndView.addObject("message",message);

//指向错误页面

modelAndView.setViewName("error");

return modelAndView;

}

}

三、在springmvc.xml中配置全局异常处理器

四、测试异常

在controller方法中抛出异常

@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})

public String editItems(Model model, @RequestParam(value="id",required=true,defaultValue="1")Integer items_id) throws Exception{

//调用service查询商品信息

ItemsCustom itemsCustom = itemsService.findItemsById(items_id);

if(itemsCustom == null){

//抛出异常

throw new CustomException("controller异常:商品信息输入有误!");

}

//通过形参中的model将model数据传到页面

//相当于modelAndView.addObject方法

model.addAttribute("itemsCustom",itemsCustom);

return "items/editItems";

}

在service中抛出异常

@Override

public ItemsCustom findItemsById(int id) throws Exception {

Items items = itemsMapper.selectByPrimaryKey(id);

if(items == null){

throw new CustomException("service异常:商品信息为空!");

}

//中间对商品信息进行业务处理

//.......

//返回itemsCustom

ItemsCustom itemsCustom = null;

if(items != null){

itemsCustom = new ItemsCustom();

//将items中的属性值拷贝到itemsCustom

BeanUtils.copyProperties(items, itemsCustom);

}

return itemsCustom;

}

不论实在service中还是在controller中抛出的自定义的异常,springmvc都会去调用自定义的异常处理器进行异常的处理

当然,如果与业务功能相关的异常,建议在service中抛出异常

与业务功能没有关系的异常,建议在controller中抛出异常

项目结构

b97678c1cd0dd710c7dded31af549e38.png

7d90e56147b06756f6272f1cb0156226.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值