简体   繁体   English

Java:未捕获Throwable

[英]Java : Throwable not caught

I got this code in java : 我在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");
    }  
}

I would expect the catch Throwable to catch any exception - error. 我希望catch Throwable可以捕获任何异常-错误。 However, the output is as follows: 但是,输出如下:

 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)

Why isn't the exception caught and how can I catch it ? 为什么没有捕获到异常,我该如何捕获?

The stack trace you've provided shows that the problem is actually not in directly loading the "com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing" class, it does find that one. 您提供的堆栈跟踪信息表明问题实际上出在直接加载"com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing"类上,它确实找到了那个。 But, in the process of loading that class, it tries to also load com/sun/deploy/ui/DialogTemplate , and that's the part where it's failing. 但是,在加载该类的过程中,它还会尝试加载com/sun/deploy/ui/DialogTemplate ,这就是失败的部分。

I googled "com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing" and ran into what I believe is the source code of this class you're trying to load. 我用谷歌搜索"com.sun.deploy.uitoolkit.impl.fx.ui.MixedCodeInSwing"并遇到了我认为是您要加载的此类的源代码。 Here is the link to the specific line of code where that in turn is trying to load the DialogTemplate thing: 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 这是到特定代码行的链接,该行进而尝试加载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

You'll notice that that particular line of code is already in a try{} block, and there already is a catch{} block afterwards which catches the ClassNotFoundException before you get a chance to catch it, and prints the stack trace at the following line of code: 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 您会注意到,特定的代码行已经在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

So, to summarize, the class you're trying to load already catches the error before you get a chance to, prints its stack trace, and does not throw the error again, so there's nothing left for you to catch 因此,总而言之,您尝试加载的类在获得机会之前已经捕获了错误,打印了其堆栈跟踪,并且不再抛出该错误,因此没有任何东西可以捕获

when I try to reproduce it, I can see that the exception is catched . 当我尝试重现它时,我可以看到捕获了异常。

but it is printed in MixedCodeInSwing class so it will show up in the console. 但它是在MixedCodeInSwing类中打印的,因此它将显示在控制台中。

snippet from MixedCodeInSwing (decompiled): 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM