自定义运行时异常

博客背景 : SpringBoot SSM

dispatch ( Controller ) 层, 不会去捕获异常, 也就是意味着, 没有 try catch 语句, 走的是统一的异常捕获处理 ( 通过 filter )

所以, 业务层不能 throw Exception, 只能抛 RuntimeException, 这样 Controller 层不需要 catch

代码如下

package com.xxx.education.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.xxx.education.entity.PrepareLesson;
import com.xxx.exception.BusinessRuntimeException;

/**
 * @ClassName: PrepareLessonService
 * @Description: PrepareLesson Service interface
 * @Author: ws
 * @date: 2020/1/2
 * @Version: 1.0.0
 */
public interface PrepareLessonService extends IService<PrepareLesson> {

    boolean deletePrepareLesson(String id) throws BusinessRuntimeException;

}

 

 

 

@Override
public boolean deletePrepareLesson(String id) throws BusinessRuntimeException {

    boolean result = false;
    try {
        // 自己的业务
        // ...
        // ...
        // ...
    } catch (BusinessRuntimeException e) {
        log.info(e.getMessage());
        // 当前类上加上 @Transactional(rollbackFor = Exception.class)
        // 事务回滚
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        throw new BusinessRuntimeException("事务回滚, 异常信息 : " + e.getLocalizedMessage());
    }
    return result;
}

 

package com.xxx.exception;

/**
 * @ClassName BusinessRuntimeException
 * @Description 业务层自定义运行时异常
 * @Author ws
 * @Date 2020/3/30
 * @Version 1.0.0
 */
public class BusinessRuntimeException extends RuntimeException {

    public BusinessRuntimeException (String message) {
        super(message);
    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值