Spring在JDBCTemplate中异常处理的实现

发布于 2024-12-27 09:20:22 字数 445 浏览 5 评论 0原文

我正在阅读一本 Spring 书来学习 Spring。在浏览了有关 JDBCTemplate 的部分后,我惊讶地发现 Spring 以不同的方式处理大多数 SQLException 异常。

例如,所有已检查异常都会转换为未检查异常。这样做到底有什么好处呢?

根据我的经验,大多数 SQL 异常都应该得到处理。例如,我们有一个与 Oracle PL/SQL 过程对话的应用程序。对 PL/SQL 过程的调用可能会返回 ORA-01403:未找到数据 异常。通常,您可以通过向用户显示错误消息来恢复这种异常。例如,搜索结果没有返回任何内容。

如果所有异常都未经检查,即 Spring 不会强迫您处理它们,那么 Spring 中将如何处理这种已检查异常?

我知道你可以捕获 RuntimeExceptions,但我非常喜欢你被迫处理检查异常的想法。将一些已检查异常转换为未检查异常有什么好处?

I am going through a Spring book to learn Spring. Having gone through sections about the JDBCTemplate i was surprised to find that Spring handles most of the SQLException exceptions differently.

For example, all checked exceptions are converted to unchecked exceptions. What exactly is the benefit of this?

In my experience, the majority of SQL exceptions should be handled. For example, we have an application that talks to an Oracle PL/SQL procedure. The call to the PL/SQL procedure can return an ORA-01403: no data found exception. This kind of exception is one that you usually recover from by displaying an error message to the user. e.g. A search result did not return anything.

How would this kind of a checked exception be handled in Spring if all exceptions are unchecked i.e. Spring wont force you to handle them?

I know that you can catch RuntimeExceptions but i quite liked the idea that you are forced to handle checked exceptions. What benefit does converting some of the checked exceptions to unchecked exceptions provide?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一百个冬季 2025-01-03 09:20:22

是的,应该处理异常,但是在 Spring 设计中,在比每个 DAO 方法更高的级别上。事实上,每个方法中都有 SQLException 处理的设计是不干净的复制粘贴设计,当您更改某些内容时,您必须在每个地方应用更改。

处理未检查异常的需求多种多样,也有不同的地方。其中之一是方面,例如,您可以将 Spring 的异常转换为您的异常(未捕获的异常不需要在方法签名中声明,因此这种转换非常优雅)。在REST方法中,您可以添加通用处理程序,将错误响应返回给调用者,并且您只需在一处编写异常处理。在基于JSF/JSP的技术中,您可以在发生错误时添加自己的错误页面。

Yes, the exceptions should be handled, but in Spring design, on higher level then the each DAO method. In fact the design where in every method there is SQLException handling is unclean copy-paste design, and when you change something, you must apply the change in every place.

There are various demands, and various places where you handle unchecked exceptions. One of these are aspects, where you can for example convert Spring's exceptions to your exceptions (uncatched exceptions need not be declared in method signature, so this convertion is very elegant). In REST method you can add generic handler that will return the error responce to the caller, and you write exception handling in only one place. In JSF/JSP based technologies you can add own error page whenever error occures.

九厘米的零° 2025-01-03 09:20:22

有些人不喜欢检查异常,因为它们迫使您进行一些异常管理。我想春天的人就是这样的。

就我个人而言,我更喜欢按照预期的方式去做事情:

try {
    // execute some SQL
} catch (SQLException ex) {
    if (ex is an expected condition) {
        // deal with it: for example with a "no data found" condition
        // this might mean returning null, or throwing some kind of business exception, such as
        // NoEmployeeFoundException, or whatever
    } else {
        // this is a programming / environment error
        // throw as unchecked exception
        throw new RuntimeException(ex);
    }
}

当然,这种方法的缺点是工作量更大。好处是您已经在代码中明确说明了哪些是“预期”情况,哪些是不应该发生的。

Some people don't like checked exceptions, as they force you to do some exception management. I guess the Spring guys are of this kind.

Personally I prefer to do things as they were intended to be made:

try {
    // execute some SQL
} catch (SQLException ex) {
    if (ex is an expected condition) {
        // deal with it: for example with a "no data found" condition
        // this might mean returning null, or throwing some kind of business exception, such as
        // NoEmployeeFoundException, or whatever
    } else {
        // this is a programming / environment error
        // throw as unchecked exception
        throw new RuntimeException(ex);
    }
}

Of course the downside of this approach is that is more work. The upside is that you have explicitly stated in code which are the "expected" circumstances and which ones should not happen ever.

岁月染过的梦 2025-01-03 09:20:22

这样做的好处是不必被迫捕获或申报它们。

我不认为在用户搜索期间找不到数据是异常的,特别是在 SQL 级别。将其转变为受检查异常相当于使用异常进行广义流控制。我认为应该避免使用反模式; YMMV。

许多与 SQL 相关的错误都与代码相关;在我看来,最好是在开发过程中快速失败。

The benefit is not being forced to catch or declare them.

I'm not convinced that not finding data during user searches is exceptional, particularly at the SQL level. Turning that into a checked exception amounts to using exceptions for generalized flow control. I would consider that an anti-pattern to be avoided; YMMV.

Many SQL-related errors are code-related; IMO it's better to fail fast--during development.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文