![](/img/trans.png)
[英]Java OutOfMemoryError not caught by clauses that catch Error and 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"
类上,它确实找到了那个。 但是,在加载该类的过程中,它还会尝试加载com/sun/deploy/ui/DialogTemplate
,这就是失败的部分。
我用谷歌搜索"com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing"
并遇到了我认为是您要加载的此类的源代码。 这是到特定代码行的链接,该行进而尝试加载DialogTemplate内容: https : //github.com/barchart/barchart-javafx-study/blob/master/barchart-oracle-javafx-2.2。 7 / src目录/主/ JAVA / COM /阳光/部署/ uitoolkit / IMPL / FX / UI / MixedCodeInSwing.java#L76
您会注意到,特定的代码行已经在try {}块中,并且之后已经有了catch {}块,它在您有机会捕获ClassNotFoundException之前捕获了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
因此,总而言之,您尝试加载的类在获得机会之前已经捕获了错误,打印了其堆栈跟踪,并且不再抛出该错误,因此没有任何东西可以捕获
当我尝试重现它时,我可以看到捕获了异常。
但它是在MixedCodeInSwing类中打印的,因此它将显示在控制台中。
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.