java.lang.reflect.Constructor.getGenericExceptionTypes()
方法返回一個Type
對象的數組,它表示聲明為該Constructor
對象拋出的異常。如果底層方法在其throws
子句中不聲明異常,則返回長度為0
的數組。
聲明
以下是java.lang.reflect.Constructor.getGenericExceptionTypes()
方法的聲明。
public Type[] getGenericExceptionTypes()
返回值
一組類型,表示底層方法拋出的異常類型。
異常
- GenericSignatureFormatError - 如果通用方法簽名不符合Java虛擬機規範中指定的格式。
TypeNotPresentException - 如果底層方法的
throws
子句引用不存在的類型聲明。MalformedParameterizedTypeException - 如果底層方法的
throws
子句引用了由於任何原因無法實例化的參數化類型。
例子
以下示例顯示了java.lang.reflect.Constructor.getGenericExceptionTypes()
方法的用法。
import java.lang.reflect.Constructor;
public class getGenericExceptionTypes {
public static void main(String[] args) {
Constructor[] constructors = SampleClass.class.getConstructors();
Class[] exceptions = constructors[0].getExceptionTypes();
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