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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。