{
开启事务
//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();
}
}