小编Mas*_*Man的帖子

Java:如何捕获在 Method.invoke 中创建的异常?

当从 Method.invoke 方法调用该方法时,我似乎无法在我的代码中捕获异常。如何从方法本身内部捕获它?

void function() {
  try {
    // code that throws exception
  }
  catch( Exception e ) {
    // it never gets here!! It goes straight to the try catch near the invoke
  }
}

try {
  return method.invoke(callTarget, args);
}
catch( InvocationTargetException e ) {
  // exception thrown in code that throws exception get here!
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

java exception-handling invoke

3
推荐指数
1
解决办法
4297
查看次数

标签 统计

exception-handling ×1

invoke ×1

java ×1