java.lang.reflect.Method.getGenericExceptionTypes()
方法返回一個Type
物件陣列,表示此Method
物件宣告丟擲的異常。 如果底層方法在其throws
子句中不宣告異常,則返回長度為0
的陣列。
以下是java.lang.reflect.Method.getGenericExceptionTypes()
方法的宣告。
public Type[] getGenericExceptionTypes()
引數
返回值
異常
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