1 réponse
Bonjour,
Il faut faire un try/catch sur une InvocationTargetException et récupérer l'exception au travers de la méthode getCause (ou getTargetException).
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/InvocationTargetException.html
Exemple :
Il faut faire un try/catch sur une InvocationTargetException et récupérer l'exception au travers de la méthode getCause (ou getTargetException).
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/InvocationTargetException.html
Exemple :
import java.lang.reflect.InvocationTargetException; public class Test { public static void f() { throw new IllegalStateException("hello"); } public static void main(String[] args) throws Exception { try { Test.class.getMethod("f").invoke(null); } catch (InvocationTargetException e) { System.err.println(e.getCause()); // java.lang.IllegalStateException: hello } } }