How to invoke a method which throws an Exception using Java Reflection?

后端 未结 3 1702
终归单人心
终归单人心 2020-12-31 02:37

I would like to invoke a method, using Java Reflection.

The problem is that this method (which I wrote) throws an Exception (I created a myCustomException).

3条回答
  •  情深已故
    2020-12-31 03:19

    One way to do it:

    try { myMethod.invoke(null, myParam); }
    catch (InvocationTargetException e) { 
      try { throw e.getCause(); }
      catch (MyCustomException e) { ...}
      catch (MyOtherException e) { ...}
    }
    

提交回复
热议问题