如果方法以 invoke 启动,Exceptions 不会导致崩溃
If a method is started with invoke, Exceptions do not cause a crash
出于某种原因,如果通过 invoke()
调用方法,它抛出的未捕获异常不会导致崩溃。但是,它确实退出了该方法。 invoke
运行 它是否是另一个线程(因为异常只会使线程崩溃,对吗?),如果是这样,是否有办法发送异常或其他东西?
这是一个简单的例子:
import java.lang.reflect.InvocationTargetException;
public class Main {
public static void main(String[] args) {
try {
Class.forName("Main").getMethod("thrower").invoke(null);
}
catch (IllegalAccessException e) {}
catch (IllegalArgumentException e) {}
catch (ClassNotFoundException e) {}
catch (NoSuchMethodException e) {}
catch (InvocationTargetException e) {}
catch (SecurityException e) {}
}
public static void thrower() {
throw new RuntimeException();
}
}
来自 java.lang.reflect.Method
文档:
@exception InvocationTargetException
if the underlying method throws an exception.
您发现了一个 InvocationTargetException
, it will be thrown with a RuntimeException
原因:
catch (InvocationTargetException e) {
final Throwable cause = e.getCause();
// cause.getClass().getName() = java.lang.RuntimeException
}
它不在单独的线程中 运行。从 thrower
抛出的 RuntimeException
实际上在反射调用期间被 InvocationTargetException
包裹,随后被什么都不做的代码捕获。
的 Javadoc
InvocationTargetException is a checked exception that wraps an
exception thrown by an invoked method or constructor. As of release
1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The "target exception" that is
provided at construction time and accessed via the
getTargetException() method is now known as the cause, and may be
accessed via the Throwable.getCause() method, as well as the
aforementioned "legacy method."
出于某种原因,如果通过 invoke()
调用方法,它抛出的未捕获异常不会导致崩溃。但是,它确实退出了该方法。 invoke
运行 它是否是另一个线程(因为异常只会使线程崩溃,对吗?),如果是这样,是否有办法发送异常或其他东西?
这是一个简单的例子:
import java.lang.reflect.InvocationTargetException;
public class Main {
public static void main(String[] args) {
try {
Class.forName("Main").getMethod("thrower").invoke(null);
}
catch (IllegalAccessException e) {}
catch (IllegalArgumentException e) {}
catch (ClassNotFoundException e) {}
catch (NoSuchMethodException e) {}
catch (InvocationTargetException e) {}
catch (SecurityException e) {}
}
public static void thrower() {
throw new RuntimeException();
}
}
来自 java.lang.reflect.Method
文档:
@exception
InvocationTargetException
if the underlying method throws an exception.
您发现了一个 InvocationTargetException
, it will be thrown with a RuntimeException
原因:
catch (InvocationTargetException e) {
final Throwable cause = e.getCause();
// cause.getClass().getName() = java.lang.RuntimeException
}
它不在单独的线程中 运行。从 thrower
抛出的 RuntimeException
实际上在反射调用期间被 InvocationTargetException
包裹,随后被什么都不做的代码捕获。
InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor. As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The "target exception" that is provided at construction time and accessed via the getTargetException() method is now known as the cause, and may be accessed via the Throwable.getCause() method, as well as the aforementioned "legacy method."