- 这里选择在业务层抛出异常
 
package com.itheima.service.impl;import com.itheima.controller.Code;import com.itheima.dao.BookDao;import com.itheima.domain.Book;import com.itheima.exception.BusinessException;import com.itheima.exception.SystemException;import com.itheima.service.BookService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic 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 BusinessException(Code.BUSINESS_ERR,"请不要使用你的技术挑战我的耐性!");        }        //模拟系统异常,将可能出现的异常进行包装,转换成自定义异常        try{            int i = 1/0;        }catch (Exception e){            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器访问超时,请重试!",e);        }        return bookDao.getById(id);    }    public List<Book> getAll() {        return bookDao.getAll();    }}
- 表现层异常处理器增加
 
package com.itheima.controller;import com.itheima.exception.BusinessException;import com.itheima.exception.SystemException;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RestControllerAdvice;//@RestControllerAdvice用于标识当前类为REST风格对应的异常处理器@RestControllerAdvicepublic class ProjectExceptionAdvice {    //@ExceptionHandler用于设置当前处理器类对应的异常类型    @ExceptionHandler(SystemException.class)    public Result doSystemException(SystemException ex){        //记录日志        //发送消息给运维        //发送邮件给开发人员,ex对象发送给开发人员        return new Result(ex.getCode(),null,ex.getMessage());    }    @ExceptionHandler(BusinessException.class)    public Result doBusinessException(BusinessException ex){        return new Result(ex.getCode(),null,ex.getMessage());    }    //除了自定义的异常处理器,保留对Exception类型的异常处理,用于处理非预期的异常    @ExceptionHandler(Exception.class)    public Result doOtherException(Exception ex){        //记录日志        //发送消息给运维        //发送邮件给开发人员,ex对象发送给开发人员        return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系统繁忙,请稍后再试!");    }}
		  	
经验总结扩展阅读
           - 
              
              
            
- 
              
              
            
- 
              
              
            
- 
              
              
            
- 
              
              
                  有一句俗话:“早起的鸟儿有虫吃。|早睡早起的时间,早起的人更容易获取成功机会 
 
- 
              
              
            
- 
              
              
                  8月,4大星座财运大涨,横财正财偏财齐聚满堂,富贵满堂 
 
- 
              
              
            
- 
              
              
                  裸妆|看来看去,浓妆艳抹不算啥,还是裸妆最百搭,关键实用又简单 
 
- 
              
              
            
- 
              
              
                  健康周谈|冬天是脑梗高发季,该如何度过?医生:做好4件事,或能安稳过冬 
 
- 
              
              
            
- 
              
              
            
- 
              
              
            
- 
              
              
            
- 
              
              
            
- 
              
              
                  越南|中国男性和越南男性有啥区别?听听越南姑娘是怎么说的 
 
- 
              
              
                  2023年1月4日制作房梁黄道吉日 2023年农历腊月十三制作房梁吉日 
 
- 
              
              
            
- 
              
              
                  2023年5月定亲黄道吉日 2023年5月哪天适合定亲