0
我在我的代码中使用了反射,为用户提供了直接调用方法的机会,而无需编写if ... else语句或开关的语调,但我遇到了问题。 我有一个名为Exit()的方法,我想抛出自定义的异常,但是当我为method.invoke()行添加catch语句时,编译器告诉我那种异常从未在该语句中抛出,但用户可能会调用该方法是绝对有可能的,我如何才能找到解决此问题的解决方案?如何使用反射指定异常类型?
这里是我的反射代码
try {
methodName = input.nextLine();
Class<?> facade = StoreFacade.getInstance().getClass();
Method method = facade.getDeclaredMethod(methodName);
method.setAccessible(true);
method.invoke(StoreFacade.getInstance());
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}catch (Exceptions.EndOfSessionException e){
System.out.println(e.getMessage());
}
非常感谢你 – Carmine 2014-09-20 22:30:31