invoke调用方法抛出异常后,外部获取异常的message为何变为null了??如何可以获取方法抛出的异常信息
try{
  Method.invoke(BO,VO);
}catch(Exception e) {
  System.out.println(e.getMessage());//此处输出为空
}

解决方案 »

  1.   

    因为抛出的是NullPointerException异常
    试试e.printStackTrace();看看
      

  2.   

    输出异常的Stack一般都用e.printStackTrace();的
      

  3.   

    Exception 的public String getMessage()Returns:
    the detail message string of this Throwable instance (which may be null).注意是Throwable instance。
    你可以再次抛出异常,同时指定message.
      

  4.   

    我在调用方法BO内部已经throw new Exception("程序错误"); 但在外部捕获异常时输出的getMessage()方法是null而不是“程序异常”,这是为什么??
      

  5.   

    另外 e.printStackTrace()输出也是null
      

  6.   

    To 楼主给的信息太少了啊,以下只是对你程序的猜测.如果异常已经被内部捕获,换句话说,你的这Method.invoke(BO,VO);语句根本就没有抛出异常,那么System.out.println(e.getMessage());自然就没有输出
      

  7.   

    try{
      Method.invoke(BO,VO);
    }catch(InvocationTargetException e) {
      System.out.println(e.getMessage());//此处输出为空
      throw e.getCause();
    }
      

  8.   

    多谢楼上的各位,问题已经解决了
    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(),这样可以在外部调用中捕获异常信息抛出