DataAccessException vs SQLException

前端 未结 1 2018
长情又很酷
长情又很酷 2021-02-01 06:57

I have two questions related to exception handling in Spring framework.

  1. Why is Spring framework\'s DataAccessException a runtime exception whereas

相关标签:
1条回答
  • 2021-02-01 07:33

    The reason to use DataAccessException over SQLException is that it more generally describes the problem. 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 express failures for both implementations.

    As for why this is Runtime and not a checked exception, it allows the callers to not have to explicitly handle it. It seems in my experience that if an SQLException or DataAccessException is thrown there's not much I can or want to do about it other than let it bubble up to somebody that can. Having to declare the throwables at each layer is more burden on the caller. If one of them cares to catch and handle it, they can.

    Here are the JavaDocs (thanks @Tom!)

    1. DataAccesssException
    2. SQLException
    0 讨论(0)
提交回复
热议问题