理解 java 中的 java.lang.reflect.invocationtargetexception 错误
在本文中,我们将了解 java 中的 java.lang.reflect.initationtargetexception。
java 中的 java.lang.reflect.initationtargetexception 错误
当开发人员使用 java reflection api 时,java.lang.reflect.initationtargetexception
是一个非常常见的异常。 检查的异常保存由调用的方法或构造函数抛出的异常。
从 jdk 1.4 版本开始,该异常已被改进以符合通用异常链机制。 简而言之,每当开发人员尝试使用 method.invoke()
调用一个类时,我们都会收到 invocabletargetexception ,并且它被 java.lang.reflect.invoketargetexception
包裹。
java.lang.reflect.invocabletargetexception错误的原因
incationtargetexception 主要发生在开发人员使用反射层并尝试调用本身引发底层异常的构造函数或方法时。 因此,java 反射 api 将方法抛出的异常包装为 invoicationtargetexception。
让我们看一个代码示例来更好地理解它。
示例代码:
import java.lang.reflect.*;
public class test {
public int dividebyzero() {
return 89/ 0;
}
public static void main(string[] args) throws nosuchmethodexception, illegalaccessexception {
test obj = new test();
method method = test.class.getmethod("dividebyzero");
try {
method.invoke(obj);
} catch (invocationtargetexception e) {
e.printstacktrace();
}
}
}
输出:
java.lang.reflect.invocationtargetexception
at java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke0(native method)
at java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:77)
at java.base/jdk.internal.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)
at java.base/java.lang.reflect.method.invoke(method.java:568)
at test.main(test.java:13)
caused by: java.lang.arithmeticexception: / by zero
at test.dividebyzero(test.java:6)
... 5 more
修复 java 中的 java.lang.reflect.initationtargetexception 错误
根据上面的内容,我们了解到底层异常是导致 java.lang.reflect.initationtargetexception
错误的原因。 我们可以使用 throwable 类的 getcause()
方法获取有关底层异常的更多信息。
因此,解决 incationtargetexception 涉及查找并解决底层异常。
示例代码:
import java.lang.reflect.*;
public class test {
public int dividebyzero() {
return 89/ 0;
}
public static void main(string[] args) throws nosuchmethodexception, illegalaccessexception
{
test obj = new test();
method method = test.class.getmethod("dividebyzero");
try {
method.invoke(obj);
}
catch (invocationtargetexception e)
{
system.out.println(e.getcause());
}
}
}
输出:
java.lang.arithmeticexception: / by zero
在上面的输出中,实际的底层异常是 arithmeticexception,因为我们除以零而发生。
一旦我们修复了底层异常,initationtargetexception 也得到了解决。 以下是完整的工作代码,无一例外; 我们刚刚删除了除以零的部分。
完整源代码:
import java.lang.reflect.*;
public class test {
public int dividebyzero() {
return 89/ 9;
}
public static void main(string[] args) throws nosuchmethodexception, illegalaccessexception
{
test obj = new test();
method method = test.class.getmethod("dividebyzero");
try {
method.invoke(obj);
}
catch (invocationtargetexception e)
{
e.printstacktrace();
}
}
}
总结
在本文中,我们了解了在 java 中使用反射层时如何包装底层异常。 我们了解了在使用 java.lang.reflect.initationtargetexception
时如何获取底层异常以及如何解决它。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
java 错误 java.net.socketexception: network is unreachable
发布时间:2023/07/16 浏览次数:963 分类:java
-
今天我们就来讨论一下java编程时出现java.net.socketexception: network is unreachable异常的可能原因及解决方法。java中出现java.net.socketexception: network is unreachable的可能原因及ag捕鱼王app官网的解决方案
java 错误 java.net.connectexception: connection timed out
发布时间:2023/07/16 浏览次数:235 分类:java
-
本篇文章将重点介绍如何使用此包进行基本的网络调用以及可能面临和解决的错误。在 java 中使用 java.net 进行网络调用 进行网络调用是 java 开发人员每天面临的最重要的事情之一。
java 中错误 attempt to invoke virtual method on a null object reference
发布时间:2023/07/16 浏览次数:948 分类:java
-
本篇文章介绍如何解决 java 中的 attempt to invoke virtual method on a null object reference 错误。java 中 attempt to invoke virtual method on a null object reference 错误
java 错误 java.security.invalidkeyexception: illegal key size
发布时间:2023/07/15 浏览次数:644 分类:java
-
本篇文章介绍包含 java.security.invalidkeyexception: illegal key size 的 java 代码。 然后,我们将了解其可能的原因。最后,它通过消除指定的错误来引导我们找到ag捕鱼王app官网的解决方案。
发布时间:2023/07/15 浏览次数:165 分类:java
-
本篇文章介绍如何解决 java 中的 java.sql.sqlexception: access denied for user 'root'@'localhost' 错误。修复 java 中的 java.sql.sqlexception: access denied for user 'root'@'localhost' (using password: yes)
发布时间:2023/07/15 浏览次数:885 分类:java
-
本篇文章介绍了 java 中 java gateway process exited before sending the driver its port number 错误 java gateway process exited before sending the driver its port number 错误
修复 java 中 java.net.bindexception: address already in use: bind 错误
发布时间:2023/07/15 浏览次数:250 分类:java
-
本篇文章介绍了 java 中的 java.net.bindexception:address already in use: bind 错误。修复java 中的 java.net.bindexception:address already in use: bind
java 中错误 unsupported major minor version
发布时间:2023/07/14 浏览次数:133 分类:java
-
出现 unsupported major.minor version 错误或 java.lang.unsupportedclassversionerror 的原因是运行时 jdk 较低,编译时 jdk 较高。 本篇文章介绍如何解决java中不支持的major.minor版本。
java 错误 error:java: javactask: source release 1.8 requires target release 1.8
发布时间:2023/07/14 浏览次数:274 分类:java
-
在使用intellij for java时,无法编译java程序是一个常见的问题。 本教程提供了此错误的ag捕鱼王app官网的解决方案。error:java: javactask: source release 1.8 requires target release 1.8 错误