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