Spring框架将检查异常包装在RuntimeExceptions中

发布于 2024-07-20 08:11:39 字数 759 浏览 5 评论 0原文

调用此方法:

   ->
    simpleJdbcTemplate.queryForInt(SQL,null);
   ->

springs SimpleJdbcTemplate 中的 queryForInt() 方法会抛出 DataAccessException,这是一个运行时异常。 我想将异常传播到应用程序的视图层,因为 Spring 框架将检查的异常包裹在 RuntimeExceptions 中,我卡在这里。

我该怎么做呢?

解释 1:

Spring 框架的 JDBC 抽象框架提供的增值 - 他们说 Spring 框架会处理除 3 和 6 之外的所有内容。3 和 6 需要由应用程序开发人员进行编码

  1. 定义连接参数

  2. 打开连接

  3. 指定语句

  4. 准备并执行语句

  5. 设置循环以迭代结果(如果有)

  6. 为每次迭代执行工作

  7. < p>处理任何异常

  8. 处理事务

  9. 关闭连接

但是如果我遇到与数据库的连接丢失的情况程序开始的某个时间。 然后,当调用上述方法时,将引发运行时异常。因为我不处理该异常,所以无法通知用户界面(视图)。

Have this method call:

   ->
    simpleJdbcTemplate.queryForInt(SQL,null);
   ->

queryForInt() method in the springs SimpleJdbcTemplate throws a DataAccessException which is a runtime exception. I want to propegate exceptions to the view tier of the application since Spring frame work Wraps Checked Exceptions inside RuntimeExceptions I stuck here.

How do I do this?

Explanation 1:

The value-add provided by the Spring Framework's JDBC abstraction framework- they say The Spring Framework takes care of all except 3 and 6. 3 and 6 need to be coded by an application developer

  1. Define connection parameters

  2. Open the connection

  3. Specify the statement

  4. Prepare and execute the statement

  5. Set up the loop to iterate through the results (if any)

  6. Do the work for each iteration

  7. Process any exception

  8. Handle transactions

  9. Close the connection

But if I encounter a situation where the connection to the database losses after certain time the program started. Then a runtime exception will be thrown when a call to the above method made.since I don't handle the exception I cannot inform the user interface (view).

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

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

发布评论

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

评论(3

水染的天色ゝ 2024-07-27 08:11:39

仅仅因为 Spring 抛出运行时异常并不意味着您无法捕获它。 如果您想对 DataAccessExceptions 做一些特殊的事情,您当然可以这样做:

try {
    // query logic
} catch (DataAccessException ex) {
    // handle the exception
}

如果您使用 Spring 的 MVC 框架,那么可能值得研究一下 ExceptionResolver 接口。 它是一种决定如何处理应用程序较低层抛出的所有未捕获异常的机制。 它为您提供了最后一次机会,可以根据抛出的异常显示不同的视图。

Just because Spring throws a runtime exception doesn't mean you cannot catch it. If you want to do something special for DataAccessExceptions, you can certainly do that:

try {
    // query logic
} catch (DataAccessException ex) {
    // handle the exception
}

If you're using Spring's MVC framework, it may be worth looking into the ExceptionResolver interface. It's a mechanism for deciding how to handle all those uncaught exceptions thrown by the lower layers of the application. It gives you one last chance to display a different view based on exceptions that are thrown.

┼── 2024-07-27 08:11:39

这取决于您的视图层是否捕获已检查异常(不属于 RuntimeException 或 Error 子类的任何 throwable 子类,或者不是直接 RuntimeException 或 Error 的实例)或未检查异常(RuntimeException 或 Errors 或这些 Throwable 子类的子类)。

一般来说,您要么会遇到这样的情况:

try {
//... processing
} catch(Exception/RuntimeException e) {
// propagate the exception to the view in a meaningful manner
}

如果是这种情况,对于运行时异常,您不必执行任何操作 - 该块将捕获运行时异常。

如果您想将其转换为检查型异常,假设您使用的是支持包装异常的 Java 版本,那么您所要做的就是:

try {
//...spring code
} catch(DataAccessException e) {
throw new Exception(e);
}

然后,在此处理之上的层会将其捕获为检查型异常。

It depends if your view tier catches checked exceptions (any subclass of throwable that does not subclass RuntimeException or Error, or are not instances of RuntimeException or Error directly) or unchecked exceptions (RuntimeException or Errors or subclasses of these Throwable subclasses).

Generally, you'll either have something like this:

try {
//... processing
} catch(Exception/RuntimeException e) {
// propagate the exception to the view in a meaningful manner
}

If this is the case, for a runtime exception, you don't have to do anything - the block will catch the runtime exception.

If you want to convert it to checked, assuming you're using a version of Java that supports wrapped exceptions, all you have to do is:

try {
//...spring code
} catch(DataAccessException e) {
throw new Exception(e);
}

Then, your layer above this processing will catch it as a checked exception.

全部不再 2024-07-27 08:11:39

您是否只想能够访问视图中的原始异常信息? 如果是这样,您应该能够对 RuntimeException 调用 getCause() 来获取导致该异常的底层已检查异常。 否则,您需要向使用 SimpleJdbcTemplate 的方法添加“抛出”声明,捕获 DataAccessException 并重新抛出所包装的已检查异常。

Do you just want to be able to access the original exception information in your View? If so, you should be able to invoke getCause() on the RuntimeException to get the underlying checked Exception that caused it. Otherwise you would need to add a "throws" declaration to your methods that are utilizing SimpleJdbcTemplate, catch DataAccessException and rethrow the checked Exceptions that are wrapped.

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