Java Reflection - Java Constructor .getExceptionTypes ()








Syntax

Constructor.getExceptionTypes() has the following syntax.

public Class <?>[] getExceptionTypes()

Example

In the following code shows how to use Constructor.getExceptionTypes() method.

import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
/*  www .  java2 s .c  om*/
public class Main {
  public static void main(String[] args) throws ClassNotFoundException {
    Class c = Class.forName("java.lang.String");

    Constructor[] constructors = c.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++) {
      print_method_or_constructor(constructors[i]);
    }

  }

  public static void print_method_or_constructor(Member member) {
    Constructor c = (Constructor) member;
    Class[] parameters = c.getParameterTypes();
    Class[] exceptions = c.getExceptionTypes();
    System.out.println(c.getDeclaringClass());

    for (int i = 0; i < parameters.length; i++) {
      System.out.println(parameters[i]);
    }
    for (int i = 0; i < exceptions.length; i++) {
      System.out.println(exceptions[i]);
    }
  }
}

The code above generates the following result.