java.lang.reflect.Method.getGenericExceptionTypes()方法示例

java.lang.reflect.Method.getGenericExceptionTypes()方法返回一个Type对象数组,表示此Method对象声明抛出的异常。 如果底层方法在其throws子句中不声明异常,则返回长度为0的数组。

声明

以下是java.lang.reflect.Method.getGenericExceptionTypes()方法的声明。

public Type[] getGenericExceptionTypes()

参数

  • NA

返回值

  • 一组类型,表示底层方法抛出的异常类型。

异常

  • GenericSignatureFormatError - 如果通用方法签名不符合Java虚拟机规范中指定的格式。

  • TypeNotPresentException - 如果底层方法的throws子句引用不存在的类型声明。

  • MalformedParameterizedTypeException - 如果底层方法的throws子句引用了由于任何原因无法实例化的参数化类型。

以下示例显示java.lang.reflect.Method.getGenericExceptionTypes()方法的用法。

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

public class MethodDemo {

   public static void main(String[] args) {
      Method[] methods = SampleClass.class.getMethods();
      Type[] exceptions = methods[0].getGenericExceptionTypes();
      for (int i = 0; i < exceptions.length; i++) {
         System.out.println(exceptions[i]);
      }
   }
}

class SampleClass {
   private String sampleField;

   public String getSampleField() throws ArrayIndexOutOfBoundsException {
      return sampleField;
   }

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField; 
   } 
}

让我们编译并运行上面的程序,这将产生以下结果 -

class java.lang.ArrayIndexOutOfBoundsException

上一篇: java.lang.reflect.Method类 下一篇: java.lang.reflect.Modifier类