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