Spring框架将检查异常包装在RuntimeExceptions中
调用此方法:
->
simpleJdbcTemplate.queryForInt(SQL,null);
->
springs SimpleJdbcTemplate
中的 queryForInt()
方法会抛出 DataAccessException
,这是一个运行时异常。 我想将异常传播到应用程序的视图层,因为 Spring 框架将检查的异常包裹在 RuntimeExceptions
中,我卡在这里。
我该怎么做呢?
解释 1:
Spring 框架的 JDBC 抽象框架提供的增值 - 他们说 Spring 框架会处理除 3 和 6 之外的所有内容。3 和 6 需要由应用程序开发人员进行编码
定义连接参数
打开连接
指定语句
准备并执行语句
设置循环以迭代结果(如果有)
为每次迭代执行工作
- < p>处理任何异常
处理事务
关闭连接
但是如果我遇到与数据库的连接丢失的情况程序开始的某个时间。 然后,当调用上述方法时,将引发运行时异常。因为我不处理该异常,所以无法通知用户界面(视图)。
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
Define connection parameters
Open the connection
Specify the statement
Prepare and execute the statement
Set up the loop to iterate through the results (if any)
Do the work for each iteration
Process any exception
Handle transactions
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅仅因为 Spring 抛出运行时异常并不意味着您无法捕获它。 如果您想对 DataAccessExceptions 做一些特殊的事情,您当然可以这样做:
如果您使用 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:
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.
这取决于您的视图层是否捕获已检查异常(不属于 RuntimeException 或 Error 子类的任何 throwable 子类,或者不是直接 RuntimeException 或 Error 的实例)或未检查异常(RuntimeException 或 Errors 或这些 Throwable 子类的子类)。
一般来说,您要么会遇到这样的情况:
如果是这种情况,对于运行时异常,您不必执行任何操作 - 该块将捕获运行时异常。
如果您想将其转换为检查型异常,假设您使用的是支持包装异常的 Java 版本,那么您所要做的就是:
然后,在此处理之上的层会将其捕获为检查型异常。
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:
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:
Then, your layer above this processing will catch it as a checked exception.
您是否只想能够访问视图中的原始异常信息? 如果是这样,您应该能够对 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.