DataAccessException vs SQLException

I have two questions related to exception handling within Spring.

1) Why is the Spring framework DataAccessException thrown at runtime, while Core Java SQLException thrown exception?

2) What advantage does Spring exception handling offer with Java handlnig exception mechanism?

+7
java spring exception exception-handling
source share
1 answer

The reason for using DataAccessException over SQLException is that it describes the problem in more detail. If you have a repository or DAO interface that has two different implementations: one for Oracle and one for Cassandra, you can have this one exception for expressions for both implementations.

As to why this is a runtime rather than a checked exception, this allows callers not to explicitly handle it. It seems, in my experience, if a SQLException or a DataAccessException , I cannot or do not want to do this, except that it can turn into someone who can do it. The requirement to declare throws on each layer is more of a burden to the caller. If one of them cares to catch and process it, they can.

Here are the JavaDocs (thanks @Tom!)

+7
source share

All Articles