lundi 12 février 2018

If a method is started with invoke, Exceptions do not cause a crash

For some reason, if an method is called via invoke(), uncaught exceptions it throws do not cause a crash. However, it does exit the method. Does invoke run it another thread (because exceptions just crash the thread, right?), and if so is there a way to send the exception up or something?

Here is a simple example:

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





Aucun commentaire:

Enregistrer un commentaire