我有一个java应用程序试图在表中插入一行和com.ibatis.common.jdbc.exception.NestedSQLException抛出原因com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException
当我尝试为唯一键约束插入dublicate数据时.
我如何捕获该异常?
解决方法:
要找到根本原因,你可以这样做:
try {
//insert
} catch (NestedSQLException e) {
Throwable t = e;
while(t.getCause() != null) {
t = t.getCause();
}
//in your situation, now t should be MySQLIntegrityConstraintViolationException
if (t instanceOf MySQLIntegrityConstraintViolationException) {
//do something
}
}