Java 正在尝试从exception.cause获取嵌套异常
我有一个try-catch块,如下所示:Java 正在尝试从exception.cause获取嵌套异常,java,exception,kotlin,Java,Exception,Kotlin,我有一个try-catch块,如下所示: try { code that may throw an exception } catch (e: DataIntegrityException) { val cause = e.cause //prints "Is the cause a ConstraintViolationException: false" println("Is the cause a ConstraintViolationException: " (c
try {
code that may throw an exception
} catch (e: DataIntegrityException) {
val cause = e.cause
//prints "Is the cause a ConstraintViolationException: false"
println("Is the cause a ConstraintViolationException: " (cause is ConstraintViolationException))
//prints org.hibernate.exception.ConstraintViolationException
println(cause)
//Here I want to do something with a ConstraintViolationException.
}
我捕获的DataIntegrityException有一个嵌套的ConstraintViolationException,我需要使用cause.constraintViolations
我已尝试直接捕获ConstraintViolationException,但由于它位于DataIntegrityViolation内,因此不会触发捕获
当
println(cause)
表明它是一个ConstraintViolationException时,我很困惑为什么(cause是ConstraintViolationException)
的计算结果是false
?请注意,还有一个…是的!谢谢,有两个同名的进口货