gpt4 book ai didi

java - 检查 ConstraintViolationException 的原因

转载 作者:行者123 更新时间:2023-12-01 18:35:50 26 4
gpt4 key购买 nike

在 neo4j 2.0 中,如果您尝试对已经具有约束的索引施加约束,则会抛出 ConstraintViolationException,这是由 AlreadyConstrainedException 引起的

我的问题是:如何检查 ConstraintViolationException 是由 ALreadyConstrainedException 引起的还是其他原因?我花了几个小时在谷歌上搜索并尝试不同的东西。下面的代码可以工作,但感觉很hacky,所以我想知道是否有更好的方法来做到这一点。

catch(ConstraintViolationException e){
if(e.getCause().getClass().getCanonicalName().equals("org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException")){
System.out.println("Index Already Constrained.");
}

}

最佳答案

我认为你的解决方案很接近,但有点难以遵循。下面的细微改进会更容易理解。我假设您的模块将导入 AlreadyConstrainedException。

catch(ConstraintViolationException e) {
if (e.getCause() instanceof AlreadyConstrainedException){
System.out.println("Index Already Constrained.");
}
}

关于java - 检查 ConstraintViolationException 的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22078470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com