Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why hibernate changed HibernateException to (unchecked) RuntimeException

I know that in some version Hibernate exceptions were changed to be unchecked. what is the reason? is this a philosophy issue or practical one?

like image 562
oshai Avatar asked Jan 05 '11 22:01

oshai


People also ask

Is Hibernateexception a checked exception?

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.

Why are runtime exceptions unchecked?

Run-time exception is called unchecked exception since it's not checked during compile time. Everything under throwable except ERROR and RuntimeException are checked exception. Adding Runtime exception in program will decrease the clarity of program.

Is it good practice to throw RuntimeException?

Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.

What is the problem with unchecked exceptions?

A checked exception must be handled either by re-throwing or with a try catch block, a runtime isn't required to be handled. An unchecked exception is a programming error and are fatal, whereas a checked exception is an exception condition within your codes logic and can be recovered or retried from.


1 Answers

Practical. So you don't have to wrap every one of your operations regarding Hibernate in try catch blocks.

Taken from 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.

like image 61
darioo Avatar answered Oct 09 '22 05:10

darioo