Java : Throwable not caught

692 Views Asked by At

I got this code in 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. 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 ?

2

There are 2 best solutions below

1
On BEST ANSWER

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. 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.

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. 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

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

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

2
On

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.

snippet from MixedCodeInSwing (decompiled):

   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();
    }

}