我使用的遊戲框架,並具有以下代碼:爲什麼我的ConstraintViolationException不被捕獲?
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報告它被拋出?爲什麼是這樣?我想抓住它,處理它並繼續正常的應用程序。
renderError做什麼?是否有可能author.name爲空,生成一個NPE? – PaulJWilliams