java.lang.reflect.Method.getExceptionTypes()
方法返回一個Class
對象數組,該數組表示由此Method
對象表示的底層方法聲明拋出的異常類型。如果方法在其throws
子句中不聲明異常,則返回長度為0
的數組。
聲明
以下是java.lang.reflect.Method.getExceptionTypes()
方法的聲明。
public Class<?>[] getExceptionTypes()
參數
- NA
返回值
- 被該對象表示的方法拋出的異常類型。
異常
- NA
以下示例顯示java.lang.reflect.Method.getExceptionTypes()
方法的用法。
import java.lang.reflect.Method;
public class MethodDemo {
public static void main(String[] args) {
Method[] methods = SampleClass.class.getMethods();
Class[] exceptions = methods[0].getExceptionTypes();
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