Java : Throwable 未被捕获
Java : Throwable not caught
我在 java 中得到了这个代码:
public static void main(String[] args) {
try{
Class tryLoadingClass = Class.forName("com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing");
}
catch (Throwable t){
System.out.println("we caught a throwable");
}
}
我希望 catch Throwable 能够捕获任何异常 - 错误。但是,输出如下:
java.lang.ClassNotFoundException: com/sun/deploy/ui/DialogTemplate
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing.<clinit> (MixedCodeInSwing.java:55)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at testrandomjavacode.TestRandomjavaCode.main(TestRandomjavaCode.java:19)
为什么没有捕获到异常,我该如何捕获它?
您提供的堆栈跟踪显示问题实际上不在于直接加载 "com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing"
class,它确实找到了那个。但是,在加载 class 的过程中,它还尝试加载 com/sun/deploy/ui/DialogTemplate
,这就是它失败的部分。
我在 "com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing"
和 运行 上搜索了我认为是您要加载的 class 的源代码。这是特定代码行的 link,它又试图加载 DialogTemplate 东西:https://github.com/barchart/barchart-javafx-study/blob/master/barchart-oracle-javafx-2.2.7/src/main/java/com/sun/deploy/uitoolkit/impl/fx/ui/MixedCodeInSwing.java#L76
您会注意到该特定代码行已经在 try{} 块中,并且之后已经有一个 catch{} 块,它在您有机会捕获它之前捕获 ClassNotFoundException,并打印以下代码行的堆栈跟踪:https://github.com/barchart/barchart-javafx-study/blob/master/barchart-oracle-javafx-2.2.7/src/main/java/com/sun/deploy/uitoolkit/impl/fx/ui/MixedCodeInSwing.java#L105
因此,总而言之,您尝试加载的 class 已经在您有机会之前捕捉到错误,打印其堆栈跟踪,并且不会再次抛出错误,所以什么都没有了等你来抓
当我尝试重现它时,我可以看到捕获到异常 。
但是它是在 MixedCodeInSwing 中打印的 class 所以它会显示在控制台中。
来自 MixedCodeInSwing 的片段(反编译):
static {
try {
tClass = Class.forName("com.sun.deploy.ui.DialogTemplate", true, (ClassLoader)null);
cMethod = tClass.getDeclaredConstructor(AppInfo.class, Component.class, String.class, String.class, Boolean.TYPE);
cMethod.setAccessible(true);
setContentMethod = tClass.getDeclaredMethod("setMixedCodeContent", String.class, Boolean.TYPE, String.class, String.class, String.class, String.class, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE, String.class);
setContentMethod.setAccessible(true);
getDialogMethod = tClass.getDeclaredMethod("getDialog");
getDialogMethod.setAccessible(true);
setVisibleMethod = tClass.getDeclaredMethod("setVisible", Boolean.TYPE);
setVisibleMethod.setAccessible(true);
disposeMethod = tClass.getDeclaredMethod("disposeDialog");
disposeMethod.setAccessible(true);
getAnswerMethod = tClass.getDeclaredMethod("getUserAnswer");
getAnswerMethod.setAccessible(true);
sysUtils = Class.forName("sun.plugin.util.PluginSysUtil", false, (ClassLoader)null);
createSysThreadMethod = sysUtils.getMethod("createPluginSysThread", Runnable.class);
} catch (Exception var1) {
var1.printStackTrace();
}
}
我在 java 中得到了这个代码:
public static void main(String[] args) {
try{
Class tryLoadingClass = Class.forName("com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing");
}
catch (Throwable t){
System.out.println("we caught a throwable");
}
}
我希望 catch Throwable 能够捕获任何异常 - 错误。但是,输出如下:
java.lang.ClassNotFoundException: com/sun/deploy/ui/DialogTemplate
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing.<clinit> (MixedCodeInSwing.java:55)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at testrandomjavacode.TestRandomjavaCode.main(TestRandomjavaCode.java:19)
为什么没有捕获到异常,我该如何捕获它?
您提供的堆栈跟踪显示问题实际上不在于直接加载 "com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing"
class,它确实找到了那个。但是,在加载 class 的过程中,它还尝试加载 com/sun/deploy/ui/DialogTemplate
,这就是它失败的部分。
我在 "com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing"
和 运行 上搜索了我认为是您要加载的 class 的源代码。这是特定代码行的 link,它又试图加载 DialogTemplate 东西:https://github.com/barchart/barchart-javafx-study/blob/master/barchart-oracle-javafx-2.2.7/src/main/java/com/sun/deploy/uitoolkit/impl/fx/ui/MixedCodeInSwing.java#L76
您会注意到该特定代码行已经在 try{} 块中,并且之后已经有一个 catch{} 块,它在您有机会捕获它之前捕获 ClassNotFoundException,并打印以下代码行的堆栈跟踪:https://github.com/barchart/barchart-javafx-study/blob/master/barchart-oracle-javafx-2.2.7/src/main/java/com/sun/deploy/uitoolkit/impl/fx/ui/MixedCodeInSwing.java#L105
因此,总而言之,您尝试加载的 class 已经在您有机会之前捕捉到错误,打印其堆栈跟踪,并且不会再次抛出错误,所以什么都没有了等你来抓
当我尝试重现它时,我可以看到捕获到异常 。
但是它是在 MixedCodeInSwing 中打印的 class 所以它会显示在控制台中。
来自 MixedCodeInSwing 的片段(反编译):
static {
try {
tClass = Class.forName("com.sun.deploy.ui.DialogTemplate", true, (ClassLoader)null);
cMethod = tClass.getDeclaredConstructor(AppInfo.class, Component.class, String.class, String.class, Boolean.TYPE);
cMethod.setAccessible(true);
setContentMethod = tClass.getDeclaredMethod("setMixedCodeContent", String.class, Boolean.TYPE, String.class, String.class, String.class, String.class, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE, String.class);
setContentMethod.setAccessible(true);
getDialogMethod = tClass.getDeclaredMethod("getDialog");
getDialogMethod.setAccessible(true);
setVisibleMethod = tClass.getDeclaredMethod("setVisible", Boolean.TYPE);
setVisibleMethod.setAccessible(true);
disposeMethod = tClass.getDeclaredMethod("disposeDialog");
disposeMethod.setAccessible(true);
getAnswerMethod = tClass.getDeclaredMethod("getUserAnswer");
getAnswerMethod.setAccessible(true);
sysUtils = Class.forName("sun.plugin.util.PluginSysUtil", false, (ClassLoader)null);
createSysThreadMethod = sysUtils.getMethod("createPluginSysThread", Runnable.class);
} catch (Exception var1) {
var1.printStackTrace();
}
}