DataAccessException与SQLException [英] DataAccessException vs SQLException

查看:174
本文介绍了DataAccessException与SQLException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个与Spring框架中的异常处理相关的问题。

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

1)为什么Spring框架的 DataAccessException 一个运行时异常而Core Java的 SQLException 一个已检查的异常?

1) Why is Spring framework's DataAccessException a runtime exception whereas Core Java's SQLException a checked exception?

2)Spring的异常处理优于Java的异常handlnig机制有什么优势?

2) What advantage does Spring's exception handling offers over Java's exception handlnig mechanism?

推荐答案

SQLException 上使用 DataAccessException 的原因是它更一般地描述了问题。如果你有一个Repository或DAO接口有两个不同的实现,一个用于Oracle,一个用于Cassandra,你可以让这两个实现的这个异常表示失败。

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 implementation, one for Oracle and one for Cassandra, you can have this one exception express failures for both implementations.

As为什么这是运行时而不是检查异常,它允许调用者不必显式处理它。根据我的经验,如果抛出 SQLException DataAccessException ,那么我无能为力或者想做什么除了让它冒泡到可以的人。必须在每一层声明throwables对调用者来说是更多的负担。如果其中一个人关心并处理它,他们可以。

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.

这是JavaDocs(感谢@Tom!)

Here are the JavaDocs (thanks @Tom!)


  1. DataAccesssException

  2. SQLException

  1. DataAccesssException
  2. SQLException

这篇关于DataAccessException与SQLException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆