try {
Class c = bean.getClass();
Method method = c.getMethod(getMethodName(task.getTarget()), getMethodParamTypes(task.getTarget()));
method.invoke(bean, getParams(task.getTarget(),task.getParams()));
}catch(InvocationTargetException ex){
Class c = bean.getClass();
Method method = c.getMethod(getMethodName(task.getTarget()), getMethodParamTypes(task.getTarget()));
method.invoke(bean, getParams(task.getTarget(),task.getParams()));
}catch(InvocationTargetException ex){
ex.printStackTrace();
throw new RuntimeException("调用接口失败,失败原因:"+ex.getTargetException() == null ? ex.getMessage() : ex.getTargetException().getMessage());}
如题,反射的地方的代码,还是会对你的自定义异常再一次封装成反射调用失败的异常。 所以如果要得到自己自定义的异常必须要用通过ex.getTargetException()来得到

本文深入探讨了Java反射机制的应用,并详细解释了如何在反射调用中封装自定义异常,确保错误信息的准确传达。
1032

被折叠的 条评论
为什么被折叠?



