java - 从 SQLException 异常中获取 SQLState
问题描述
我有抛出包装在 RuntimeException 中的 SQLException 的代码,例如:
} catch (RuntimeException e) {
Throwable t = e.getCause();
while ((t != null) && !(t instanceof java.sql.SQLException)) {
t = t.getCause();
}
. . . Someother code
if (SQLState==) {
//throw new custom exception based on SQLState);
}
如何在 if 循环中获取 SQLState?不知何故,while循环t为空。
解决方案
得到了答案
} catch (RuntimeException e) {
Throwable t = e.getCause();
SQLException exp;
while ((t != null) && !(t instanceof SQLException)) {
t = t.getCause();
exp= (SQLException) t;
}
if (exp.getSQLState=="....")) {
throw new custom exception based on SQLState);
}
}
推荐阅读
- swift - 如何更改 SpriteKit 中精灵的触摸偏移量?
- python - 在 pytorch 中使用量化模型没有性能提升
- azure - Azure 的 myapps 门户中面向 B2B 用户的聊天机器人
- snowflake-cloud-data-platform - 雪花:为什么在资源争用期间查询会重试两次
- r - 编写我的第一个闪亮的应用程序,如何通过选定的变量进行绘图,为什么这个脚本不起作用?
- python - 如何通过 Pygame 从 MIDI 设备获取输入?
- c++ - 将数据放在私有类中是否理想?(C++)
- javascript - 成功登录后, this.props.history.push 不会进入新页面,因为 ProtectedRoute 不成功
- javascript - 如果数组超出索引,有人可以解释以下函数会发生什么吗?
- c# - 为什么我不能从我的 Xamarin Froms 应用程序发布到我的 .net 核心 web api