- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章jdk中动态代理异常处理分析:UndeclaredThrowableException由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
背景 。
在rpc接口调用场景或者使用动态代理的场景中,偶尔会出现undeclaredthrowableexception,又或者在使用反射的场景中,出现invocationtargetexception,这都与我们所期望的异常不一致,且将真实的异常信息隐藏在更深一层的堆栈中。本文将重点分析下undeclaredthrowableexception 。
先给结论 。
使用jdk动态代理接口时,若方法执行过程中抛出了受检异常但方法签名又没有声明该异常时则会被代理类包装成undeclaredthrowableexception抛出.
问题还原 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
// 接口定义
public
interface
iservice {
void
foo()
throws
sqlexception;
}
public
class
serviceimpl
implements
iservice{
@override
public
void
foo()
throws
sqlexception {
throw
new
sqlexception(
"i test throw an checked exception"
);
}
}
// 动态代理
public
class
iserviceproxy
implements
invocationhandler {
private
object target;
iserviceproxy(object target){
this
.target = target;
}
@override
public
object invoke(object proxy, method method, object[] args)
throws
throwable {
return
method.invoke(target, args);
}
}
public
class
maintest {
public
static
void
main(string[] args) {
iservice service =
new
serviceimpl();
iservice serviceproxy = (iservice) proxy.newproxyinstance(service.getclass().getclassloader(),
service.getclass().getinterfaces(),
new
iserviceproxy(service));
try
{
serviceproxy.foo();
}
catch
(exception e){
e.printstacktrace();
}
}
}
|
运行上面的maintest,得到的异常堆栈为 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
java.lang.reflect.undeclaredthrowableexception
at com.sun.proxy.$proxy0.foo(unknown source)
at com.learn.reflect.maintest.main(maintest.java:
16
)
caused by: java.lang.reflect.invocationtargetexception
at sun.reflect.nativemethodaccessorimpl.invoke0(
native
method)
at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:
62
)
at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:
43
)
at java.lang.reflect.method.invoke(method.java:
498
)
at com.learn.reflect.iserviceproxy.invoke(iserviceproxy.java:
19
)
...
2
more
caused by: java.sql.sqlexception: i test
throw
an checked exception
at com.learn.reflect.serviceimpl.foo(serviceimpl.java:
11
)
...
7
more
|
而我们期望的是 。
1
2
3
|
java.sql.sqlexception: i test
throw
an checked exception
at com.learn.reflect.serviceimpl.foo(serviceimpl.java:
11
)
...
|
原因分析 。
在上述问题还原中,真实的sqlexception被包装了两层,先被invocationtargetexception包装,再被undeclaredthrowableexception包装。 其中,invocationtargetexception为受检异常,undeclaredthrowableexception为运行时异常。 为何会被包装呢,还要从动态代理的生成的代理类说起.
jdk动态代理会在运行时生成委托接口的具体实现类,我们通过proxygenerator手动生成下class文件,再利用idea解析class文件得到具体代理类: 截取部分:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public
final
class
iserviceproxy$
1
extends
proxy
implements
iservice {
private
static
method m1;
private
static
method m2;
private
static
method m3;
private
static
method m0;
public
iserviceproxy$
1
(invocationhandler var1)
throws
{
super
(var1);
}
public
final
void
foo()
throws
sqlexception {
try
{
super
.h.invoke(
this
, m3, (object[])
null
);
}
catch
(runtimeexception | sqlexception | error var2) {
throw
var2;
}
catch
(throwable var3) {
throw
new
undeclaredthrowableexception(var3);
}
}
static
{
try
{
m1 =
class
.forname(
"java.lang.object"
).getmethod(
"equals"
,
new
class
[]{
class
.forname(
"java.lang.object"
)});
m2 =
class
.forname(
"java.lang.object"
).getmethod(
"tostring"
,
new
class
[
0
]);
m3 =
class
.forname(
"com.learn.reflect.iservice"
).getmethod(
"foo"
,
new
class
[
0
]);
m0 =
class
.forname(
"java.lang.object"
).getmethod(
"hashcode"
,
new
class
[
0
]);
}
catch
(nosuchmethodexception var2) {
throw
new
nosuchmethoderror(var2.getmessage());
}
catch
(classnotfoundexception var3) {
throw
new
noclassdeffounderror(var3.getmessage());
}
}
}
|
在调用“委托类”的foo方法时,实际上调用的代理类iserviceproxy$1的foo方法,而代理类主要逻辑是调用invocationhandler的invoke方法。 异常处理的逻辑是,对runtimeexception、接口已声明的异常、error直接抛出,其他异常被包装成undeclaredthrowableexception抛出。 到这里,或许你已经get了,或许你有疑问,在接口实现中的确是throw new sqlexception,为什么还会被包装呢? 再来看iserviceproxy的invoke方法,它就是直接通过反射执行目标方法,问题就在这里了。 method.invoke(object obj, object... args)方法声明中已解释到,若目标方法抛出了异常,会被包装成invocationtargetexception。(具体可查看javadoc) 。
所以,串起来总结就是: 具体方法实现中抛出sqlexception被反射包装为会被包装成invocationtargetexception,这是个受检异常,而代理类在处理异常时发现该异常在接口中没有声明,所以包装为undeclaredthrowableexception.
解决方法 。
在实现invocationhandler的invoke方法体中,对method.invoke(target, args);调用进行try catch,重新 throw invocationtargetexception的cause。即:
1
2
3
4
5
6
7
8
|
@override
public
object invoke(object proxy, method method, object[] args)
throws
throwable {
try
{
return
method.invoke(target, args);
}
catch
(invocationtargetexception e){
throw
e.getcause();
}
}
|
题外话 。
为什么代理类中对未声明的受检异常转为undeclaredthrowableexception? 因为java继承原则:即子类覆盖父类或实现父接口的方法时,抛出的异常必须在原方法支持的异常列表之内。 代理类实现了父接口或覆盖父类方法 。
参考 。
https://www.ibm.com/developerworks/cn/java/j-lo-proxy1/index.html#icomments 。
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我的支持.
原文链接:https://my.oschina.net/hebaodan/blog/1584134 。
最后此篇关于jdk中动态代理异常处理分析:UndeclaredThrowableException的文章就讲到这里了,如果你想了解更多关于jdk中动态代理异常处理分析:UndeclaredThrowableException的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
在 JavaScript 中,我们可以动态创建 元素并附加到 部分,以便为大量元素应用 CSS 规则。 这种方法的优点或缺点是什么? 如果它确实提供了与元素上的 javascript 迭代相比的性
我有这个代码 import "./HTTPMethod.dart"; import '../../DataModel/DataModel.dart'; mixin RouterMixin { HT
哪些 OLAP 工具支持动态、动态地创建维度或层次结构? 例如,层次结构将成员定义为:“前 5 名”、“前 6-10 名”、“其他”... 计算成员是通常的答案,我正在寻找不同的东西。计算器的问题。成
我正在 CakePHP 中创建一个“表单编辑器”。 该界面允许用户选择要应用于字段的验证,例如数字、电子邮件等 因此,我需要根据用户输入为模型动态创建验证。为此,我可以使用验证对象:https://b
这是一个场景: 我有一个Web服务,我们将其称为部署在tomcat(轴)上的StockQuoteService。通过此 Web 服务公开了 getStockQuote() 方法。 现在,我想构建一个
我正在尝试从服务器获取 JSON 响应并将其输出到控制台。 Future login() async { var response = await http.get( Uri.
我从另一个问题中得到了这段代码(感谢 chunhunghan)。我需要创建一个登录屏幕,并尝试根据服务器发回给我的响应来验证用户凭据,但是每次我尝试运行代码时,它都会给我“未处理的异常:Interna
当我在“Dart”主程序中运行它时,一切正常,并且我得到了一个与会者列表。但是,当我在我的 Flutter 应用程序中调用它时,出现错误: flutter:“List”类型不是“List>”类型的子类
本文实例为大家分享了js实现验证码动态干扰的具体代码,供大家参考,具体内容如下 效果一 效果二 代码一 ?
目前我正在为我的网站使用 No-Ip,我想使用 cloudflare 来抵御 ddos 和机器人程序。我注意到您需要一个用于 cloudflare 的域。我还搜索了网络,发现了一个叫做 cloud
有没有办法在 Excel VBA 中构建动态 if 语句?基本上我正在尝试创建一个参数化计算,用户将能够输入不同的变量,即 变量 1 “变量 2” “变量 3” 在这种情况下 变量 1 是单元格引用
大家好, 请查看上面的图片,我有两张 table 。在下面代码的第一个表中,我得到了这种格式。 但我想像 Table2 那样格式化,每个合并单元格中的行数是动态的,而且不一样。 有没有办法像table
如何根据我添加的 View 修改标题部分的高度?heightForHeaderInSection在 viewForHeaderInSection 之前被调用我不知道 View 大小,直到我创建它。 最
是否存在在运行时生成 AST/解析树的解析器?有点像一个库,它会接受一串 EBNF 语法或类似的东西并吐出数据结构? 我知道 antlr、jlex 和他们的同类。他们生成可以做到这一点的源代码。 (喜
我在持有汽车制造商的表格上有一个 MultipleChoiceField。我想将我的汽车数据库过滤到已检查的品牌,但这会导致问题。如何动态获取所有 Q(make=...) 语句? 我如何开始:['va
$end = preg_replace($pattern, $replacement, $str); 如何使替换字符串 $replacement 随 $str 中的每次匹配而变化?例如,我想用关联的图
我正在编写一个 VBA 程序,用于过滤表中的值。我试图使其成为一个适用于您提供的所有表格的通用程序。在我的程序中,我必须设置它正在过滤的表的范围:Set rng = dataSheet.Range("
我正在循环一个元素数组,并且我想使用给定的模板递归地显示该元素 然后在该模板内使用带有切换功能的按钮来显示/隐藏给定元素的Child的更深级别模板(Child也是一个元素) 这是我的模板
从客户端(html)发送表单,服务器端通过选择选项之一决定运行哪个函数。 const decideWho = (form) => { const choice = form.choice; c
我有一个具有以下属性的按钮: circle_normal.xml(在 res/drawable 中) circle.xml(在 res/drawable 中)
我是一名优秀的程序员,十分优秀!