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.