Java Reflection method get exception method throws

Introduction

The list of exceptions a method throws is obtained using the following methods:

public Class<?>[] getExceptionTypes()  
public Type[] getGenericExceptionTypes()  

import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class Main {
   public static void main(String args[]) throws Exception {

      Class c = Class.forName("java.lang.String");
      Method[] methods = c.getDeclaredMethods();
      for (Method m : methods) {
         System.out.println("\n\nMethod: " + m.getName());

         System.out.print("\nExceptions thrown: ");
         Type[] exceptions = m.getExceptionTypes();
         for (Type e : exceptions) {
            System.out.print("  " + e);
         }/*w ww.  j  ava 2s.c  o m*/
      }
   }
}



PreviousNext

Related