java异常处理

{

开启事务

//delete...

//insert...

结束事务
}

 

项目中遇到一个问题,insert时抛除了唯一索引冲突的异常MySQLIntegrityConstraintViolationException,该类异常不需要回滚,仍然想要执行delete。但是底层的插入方法在封装的时候,仅向外抛出了一个SQLException,无法try catch到该异常,所以有什么办法可以扑获到该具体的异常呢。

如下图所示,就可以啦

package com.example.spring.manager;

import lombok.extern.log4j.Log4j2;

import java.util.concurrent.TimeoutException;

@Log4j2
public class MyTest {
    public void testException() throws Exception {
//    public void testException() {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw e;
        }
    }

    public void test() {
        try {
//            try {
                testException();
//            } catch (ArithmeticException e) {
//                System.out.println("出现算术异常了!!!");
//            }

        } catch (Exception e) {
            if (e instanceof ArithmeticException) {
                System.out.println("呵呵呵!!!");
            }
            log.error("出现未知异常了!!!", e.toString());

        }
    }

    public static void main(String[] args) {
        MyTest myTest = new MyTest();
        myTest.test();
    }
}

 


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值