Java Reflection을 사용하여 메소드를 호출하려고합니다.Java Reflection을 사용하여 예외를 throw하는 메소드를 호출하는 방법은 무엇입니까?
문제는이 메서드 (내가 작성한)가 Exception을 던졌습니다 (myCustomException). try/catch 절을 추가 할 때 Eclipse에서 "catch 절을 도달 할 수 없다"고했기 때문에 프로젝트를 실행할 수 없습니다. 가 MyCustomException, eveything 괜찮 throw하지 않습니다을 MyMethod라는 때
270. myMethod.invoke(null, myParam); // NB : null because myMethod is static
: 나는 수업 시간에 MyClass에을 MyMethod라는를 호출 할 때 다음
이다.
것은이 전 클래스 BarClass()에 fooMethod()를 호출하려고한다고 가정 해 봅시다, 그리고 : 그것은 MyCustomException을 던져 때,이 오류 메시지가
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
심지어 가능합니까? 어쨌든 도움을 주셔서 감사합니다.
것 같습니다. try-catch로 둘러 쌉니다. – Razvan