Ich möchte eine Methode mit Java Reflection aufrufen.Wie ruft man eine Methode auf, die eine Exception mit Java Reflection auslöst?
Das Problem ist, dass diese Methode (die ich schrieb) löst eine Ausnahme (ich erstellte eine myCustomException). Wenn ich eine try/catch-Klausel hinzufüge, kann ich mein Projekt nicht ausführen, weil Eclipse sagt "die catch-Klausel ist nicht erreichbar".
Hier ist, wenn ich versuche myMethod in der Klasse aufzurufen MyClass:
270. myMethod.invoke(null, myParam); // NB : null because myMethod is static
Wenn MyMethod wirft kein MyCustomException, eveything in Ordnung ist. Aber wenn es wirft einen MyCustomException, erhalte ich diese Fehlermeldung:
Sagen wir, ich versuche fooMethod aufzurufen(), die BarClass in der Klasse(), und:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.tests.MyClass.test5(270)
at com.tests.MyClass.main(297)
Caused by: com.tests.MyCustomException
at com.tests.barClass.fooMethod(BarClass.java:129)
... 6 more
Ist es überhaupt möglich? Danke trotzdem für Hilfe.
Es sieht aus wie der Methodenaufruf eine Ausnahme wirft. Umgeben Sie es mit Versuch-Fang. – Razvan