gpt4 book ai didi

java - 为什么我的 ConstraintViolationException 没有被捕获?

转载 作者:行者123 更新时间:2023-11-30 09:46:24 27 4
gpt4 key购买 nike

我正在使用 Play Framework,并具有以下代码:

import org.hibernate.exception.ConstraintViolationException;

...
public class AuthorService extends Controller

...

public static void delete(Long id)
{
Author author = Author.findById(id);
if(author == null) {
renderError("Attempt to delete author has failed! The entry could not be found.", 500);
}
try {
author.delete();
} catch (ConstraintViolationException e) {
renderError("A foreign key violation exception was thrown trying to delete <b>" + author.name + "</b>!", 500);
}
renderGSON(jobDescription);
}

...
}

我没有使用级联删除记录,因为外来项必须设置为用户稍后选择的条目。

我的问题是,从未捕获到 ContraintViolationException,但我的 IDE 报告它被抛出?为什么是这样?我想捕获它,处理它并照常继续申请。

最佳答案

因为在 Play 中,根据 code (删除调用 _delete),该方法仅抛出 javax.persistence.PersistenceException 和 RuntimeException。

关于java - 为什么我的 ConstraintViolationException 没有被捕获?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7159525/

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