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



说明

java.lang.reflect.Constructor.getGenericExceptionTypes() 方法返回一个 Type 对象数组,代表该 Constructor 对象声明抛出的异常。如果底层方法在 throws 子句中未声明任何异常,则返回长度为 0 的数组。

声明

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

public Type[] getGenericExceptionTypes()

返回

一个 Types 数组,代表底层方法抛出的异常类型。

异常

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

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

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

示例

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

现场演示
package com.tutorialspoint;

import java.lang.reflect.Constructor;
import java.lang.reflect.Type;

public class ConstructorDemo {

   public static void main(String[] args) {

      Constructor[] constructors = SampleClass.class.getConstructors();
      Type[] exceptions = constructors[0].getGenericExceptionTypes();
      for (int i = 0; i < exceptions.length; i++) {
         System.out.println(exceptions[i]);
      }
   }
}

class SampleClass {
   private String sampleField;

   public SampleClass() throws ArrayIndexOutOfBoundsException {
   }

   public SampleClass(String sampleField){
      this.sampleField = sampleField;
   }

   public String getSampleField() {
      return sampleField;
   }

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

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

class java.lang.ArrayIndexOutOfBoundsException
java_reflect_constructor.htm
广告
© . All rights reserved.