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