SSM整合项目异常处理

异常处理时很重要的,分为业务出错,用户试错,系统出错

目录

异常处理时很重要的

步骤

1.自定义项目系统级异常

2.自定义项目业务级异常

3.设置异常编码

4.触发自定义异常

5.拦截并处理异常

6.异常处理器效果对比


步骤

1.自定义项目系统级异常

package com.liyang.exception;

/**
 * @version 1.0
 * @auter liyang
 */
public class SystemException extends RuntimeException{
    private Integer code;

    public SystemException(Integer code) {
        this.code = code;
    }

    public SystemException(Integer code,String message) {
        super(message);
        this.code = code;
    }

    public SystemException(Integer code,String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}

2.自定义项目业务级异常

package com.liyang.exception;

/**
 * @version 1.0
 * @auter liyang
 */
public class BusnessSystemException extends RuntimeException{
    private Integer code;

    public BusnessSystemException(Integer code) {
        this.code = code;
    }

    public BusnessSystemException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    public BusnessSystemException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}

3.设置异常编码

package com.liyang.controller;

/**
 * @version 1.0
 * @auter liyang
 */
public class Code {
    public static final Integer SAVE_OK=20011;
    public static final Integer DELETE_OK=20021;
    public static final Integer UPDATA_OK=20031;
    public static final Integer GET_OK=20041;


    public static final Integer SAVE_ERR=20010;
    public static final Integer DELETE_ERR=20020;
    public static final Integer UPDATA_ERR=20030;
    public static final Integer GET_ERR=20040;

    public static final Integer SYSTEM_ERR=50001;
    public static final Integer SYSTEM_TIMEOUT_ERR=50002;
    public static final Integer BUSNEISS_ERR=60002;
    public static final Integer SYSTEM_UNKNOW_ERR=59999;

}

4.触发自定义异常

package com.liyang.service.impl;

import com.liyang.controller.Code;
import com.liyang.dao.BookDao;
import com.liyang.domain.Book;
import com.liyang.exception.BusnessSystemException;
import com.liyang.exception.SystemException;
import com.liyang.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    public boolean save(Book book) {
        bookDao.save(book);
        return true;
    }

    public boolean update(Book book) {
        bookDao.update(book);
        return true;
    }

    public boolean delete(Integer id) {
        bookDao.delete(id);
        return true;
    }

    public Book getById(Integer id) {
        //将可能出现的异常进行包装,转换成自定义异常
        if(id==1){
            throw new BusnessSystemException(Code.BUSNEISS_ERR,"请不要用你的技术挑战我的耐心");
        }


        try{
            int i=1/0;
        }catch (Exception e){
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器超时,请重试!");
        }

        return bookDao.getById(id);
    }

    public List<Book> getAll() {
        return bookDao.getAll();
    }
}

5.拦截并处理异常

package com.liyang.controller;

import com.liyang.exception.BusnessSystemException;
import com.liyang.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @version 1.0
 * @auter liyang
 */
@RestControllerAdvice
public class ProjectExceptionAdvice {

    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex){
        //发日志
        //发邮件给开发人员
        //发短信给运维人员
        
        return new Result(ex.getCode(),null,ex.getMessage());
    }

    @ExceptionHandler(BusnessSystemException.class)
    public Result doBusnessSystemException(BusnessSystemException ex){
        //发日志
        //发邮件给开发人员
        //发短信给运维人员

        return new Result(ex.getCode(),null,ex.getMessage());
    }




    @ExceptionHandler(Exception.class)
    public Result doException(Exception ex){

        return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系统繁忙,请稍后再试");
    }
}

6.异常处理器效果对比

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值