我知道在某些版本中,Hibernate 异常被更改为未检查。是什么原因?这是哲学问题还是实践问题?
最佳答案
实用。因此,您不必将有关 Hibernate 的每一项操作都包装在 try catch block 中。
摘自 Java Persistence with Hibernate:
A history of exceptions — Exceptions and how they should be handled always end in heated debates between Java developers. It isn’t surprising that Hibernate has some noteworthy history as well. Until Hibernate 3.x, all exceptions thrown by Hibernate were checked exceptions, so every Hibernate API forced the developer to catch and handle exceptions. This strategy was influenced by JDBC , which also throws only checked exceptions. However, it soon became clear that this doesn’t make sense, because all exceptions thrown by Hibernate are fatal. In many cases, the best a developer can do in this situation is to clean up, display an error message, and exit the application. Therefore, starting with Hibernate 3.x, all exceptions thrown by Hibernate are subtypes of the unchecked Runtime Exception, which is usually handled in a single location in an application. This also makes any Hibernate template or wrapper API obsolete.
关于java - 为什么 hibernate 将 HibernateException 更改为(未检查的)RuntimeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4609870/