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.