当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java java.lang.reflect.Method.getGenericExceptionTypes()用法及代码示例



描述

这个java.lang.reflect.Method.getGenericExceptionTypes()方法返回一个 Type 对象数组,这些对象表示声明为由此 Method 对象抛出的异常。如果底层方法在它的 throws 子句中没有声明任何异常,则返回一个长度为 0 的数组。

声明

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

public Type[] getGenericExceptionTypes()

返回

表示底层方法抛出的异常类型的 Type 数组。

异常

  • GenericSignatureFormatError- 如果泛型方法签名不符合 Java 虚拟机规范中指定的格式。

  • TypeNotPresentException- 如果底层方法的 throws 子句引用不存在的类型声明。

  • MalformedParameterizedTypeException- 如果底层方法的 throws 子句引用了由于任何原因无法实例化的参数化类型。

示例

下面的例子展示了 java.lang.reflect.Method.getGenericExceptionTypes() 方法的用法。

现场演示
package com.tutorialspoint;

import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class MethodDemo {

   public static void main(String[] args) {

      Method[] methods = SampleClass.class.getMethods();
      Type[] exceptions = methods[0].getGenericExceptionTypes();
      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.getGenericExceptionTypes() Method Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。