【问题标题】:Get SQLState from SQLException exception从 SQLException 异常中获取 SQLState
【发布时间】:2021-10-10 12:09:00
【问题描述】:

我的代码会抛出包装在 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为null。

【问题讨论】:

    标签: java exception jdbc runtimeexception sqlexception


    【解决方案1】:

    找到答案

            } 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); 
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2021-01-31
      • 2017-11-03
      • 2012-07-19
      相关资源
      最近更新 更多