invoke调用方法抛出异常后,外部获取异常的message为何变为null了??如何可以获取方法抛出的异常信息
try{
Method.invoke(BO,VO);
}catch(Exception e) {
System.out.println(e.getMessage());//此处输出为空
}
try{
Method.invoke(BO,VO);
}catch(Exception e) {
System.out.println(e.getMessage());//此处输出为空
}
试试e.printStackTrace();看看
the detail message string of this Throwable instance (which may be null).注意是Throwable instance。
你可以再次抛出异常,同时指定message.
Method.invoke(BO,VO);
}catch(InvocationTargetException e) {
System.out.println(e.getMessage());//此处输出为空
throw e.getCause();
}
try{
Method.invoke(BO,VO);
}catch(InvocationTargetException e) {
System.out.println(e.getMessage());//此处输出为空
System.out.println(e.getCause());//此处输出为"程序错误"
throw new Exception(e.getCause());
}Method.invoke()方法将程序异常终止转化为InvocationTargetException,并将错误信息存放在 e.getCause(),这样可以在外部调用中捕获异常信息抛出