Online Java Compiler By
JavaTpoint.com
//import statement import java.lang.reflect.Method; public class ReflectMethodgetExceptionTypesExample1 { public static void main(String[] args) { try { Class classobj = demoClassX23.class; Method[] mthd = classobj.getMethods(); for (Method method : mthd) { if (method.getName().equals("setValue") || method.getName().equals("getValue")) { Class[] exceptions = method.getExceptionTypes(); System.out.println("Exception Thrown by Method: " + method.getName()); System.out.println("Exception Array length: " + exceptions.length); for (Class c : exceptions) { System.out.println(c.getName()); } } } } catch (Exception e) { e.printStackTrace(); } } } // a simple class class demoClassX23 { // throw some exception by method public void setValue(String value) throws ClassNotFoundException, ArrayIndexOutOfBoundsException, ArithmeticException { } // method throwing no exception public String getValue(String value) { return value; } }
Output