Mega Code Archive

 
Categories / Java Tutorial / Reflection
 

Call all possible exceptions during method invocation with reflection

import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Invoke {   public static void main(String[] args) {     try {       Class c = Class.forName(args[0]);       Method m = c.getMethod(args[1]);       Object ret = m.invoke(null);       System.out.println("Invoked static method: " + args[1] + " of class: " + args[0]           + " with no args\nResults: " + ret);     } catch (ClassNotFoundException e) {       System.out.println(e);     } catch (NoSuchMethodException e2) {       System.out.println(e2);     } catch (IllegalAccessException e3) {       System.out.println(e3);     } catch (InvocationTargetException e) {       System.out.println(e);       System.out.println("Method threw an: " + e.getTargetException());     }   } }