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



描述

java.lang.reflect.Method.getGenericExceptionTypes() 方法返回一個 Type 物件陣列,該陣列表示此 Method 物件宣告丟擲的異常。如果方法在其 throws 子句中宣告不丟擲任何檢查異常,則返回長度為 0 的陣列。

宣告

以下是 java.lang.reflect.Method.getGenericExceptionTypes() 方法的宣告。

public Type[] getGenericExceptionTypes()

返回

表示底層方法丟擲的異常型別的 Type 陣列。

異常

  • GenericSignatureFormatError - 如果泛型方法簽名不符合 Java 虛擬機器規範中規定的格式。

  • TypeNotPresentException - 如果底層方法的 throws 子句引用不存在的型別宣告。

  • MalformedParameterizedTypeException - 如果底層方法的 throws 子句引用因任何原因無法例項化的引數化型別。

示例

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

線上示例
package com.tutorialspoint;

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_reflect_method.htm
廣告
© . All rights reserved.