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



说明

java.lang.reflect.Method.getExceptionTypes() 方法返回一个 Class 对象数组,该数组表示此 Method 对象表示的底层方法抛出的异常类型。如果该方法的 throws 子句中未声明任何异常,则返回长度为 0 的数组。

声明

以下是 java.lang.reflect.Method.getExceptionTypes() 方法的声明。

public Class<?>[] getExceptionTypes()

返回

此对象表示的方法抛出的异常类型。

示例

以下示例展示 java.lang.reflect.Method.getExceptionTypes() 方法的使用。

package com.tutorialspoint;

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_reflect_method.htm
广告
© . All rights reserved.