當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Method類 getExceptionTypes()用法及代碼示例


“Method class”的java.lang.reflect.Method.getExceptionTypes()方法返回一個聲明為由方法對象拋出以處理方法內部異常的異常類型類對象數組。使用此方法將所有使用thrown子句處理的異常都作為Class對象的數組返回。如果應用此方法的方法在其throws子句中未聲明任何異常,則此方法返回長度為0的數組。

用法:

public Class<?>[] getExceptionTypes()

返回值:此方法返回此Method對象使用thrown子句聲明的異常類的數組



以下示例程序旨在說明Method類的getExceptionTypes()方法:

範例1:打印所有例外

/* 
* Program Demonstrate getExceptionTypes() method  
* of Method Class. 
*/
import java.lang.reflect.Method; 
  
public class GFG { 
  
    // Main method 
    public static void main(String[] args) 
    { 
        try { 
            // create class object 
            Class classobj = demoClass.class; 
  
            // get list of method Objects 
            Method[] methods = classobj.getMethods(); 
  
            // loop through list 
            for (Method method:methods) { 
  
                // check for method with there name 
                if (method.getName().equals("setValue") 
                    || method.getName().equals("getValue")) { 
                    // get Exception Types 
                    Class[] exceptions = method.getExceptionTypes(); 
  
                    // print exception Types thrown by method Object 
                    System.out.println("Exception Thrown by Method:"
                                       + method.getName()); 
                    System.out.println("Exception Array length:"
                                       + exceptions.length); 
                    for (Class c:exceptions) { 
                        System.out.println(c.getName()); 
                    } 
                } 
            } 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
} 
// a simple class 
class demoClass { 
  
    // throw some exception by method 
    public void setValue(String value) 
        throws ClassNotFoundException, 
               ArrayIndexOutOfBoundsException, 
               ArithmeticException 
    { 
    } 
  
    // method throwing no exception 
    public String getValue(String value) 
    { 
        return value; 
    } 
}
輸出:
Exception Thrown by Method:getValue
Exception Array length:0
Exception Thrown by Method:setValue
Exception Array length:3
java.lang.ClassNotFoundException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArithmeticException

範例2:檢查Method對象是否拋出某些已定義的Exception。如果是,則打印true,否則打印false。

// Program Demonstrate getExceptionTypes() method  
// Using getExceptionTypes() method of Method Class 
  
import java.lang.reflect.Method; 
  
// a simple class 
class GFGSampleClass { 
  
    String value; 
  
    // throw some exception by method 
    public void setValue(String value) 
        throws ClassNotFoundException, 
               ArrayIndexOutOfBoundsException, 
               ArithmeticException 
    { 
        this.value = value; 
    } 
} 
  
public class GFG { 
  
    // Main method 
    public static void main(String[] args) 
    { 
        try { 
            // create class object 
            Class classobj = GFGSampleClass.class; 
  
            // get list of method Objects 
            Method[] methods = classobj.getMethods(); 
  
            // loop through list 
            for (Method method:methods) { 
  
                // check for method with there name 
                if (method.getName().equals("setValue")) { 
  
                    // check whether method throw 
                    // IndexOutOfBoundsException Exception 
                    Class exceptionObj = IndexOutOfBoundsException.class; 
                    boolean response = isCertainExceptionIsThrown(method, 
                                                                  exceptionObj); 
                    System.out.println("IndexOutOfBoundsException is "
                                       + "thrown by setValue():" + response); 
                } 
            } 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
  
    /* 
    * Return true if the given method throws the  
    *     exception passed as Parameter. 
    */
    private static boolean
    isCertainExceptionIsThrown(Method method, Class<?> exceptionName) 
    { 
        // get all exception list 
        Class exceptions[] = method.getExceptionTypes(); 
  
        for (int i = 0; i < exceptions.length; i++) { 
            // check exception thrown or not 
            if (exceptions[i] == exceptionName) { 
                return true; 
            } 
        } 
  
        return false; 
    } 
}
輸出:
IndexOutOfBoundsException is thrown by setValue():false

參考:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getExceptionTypes-




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Method Class | getExceptionTypes() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。