java.lang.reflect.Method.getExceptionTypes()方法示例

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

上一篇: java.lang.reflect.Method类 下一篇: java.lang.reflect.Modifier类