方法类 | Java中的 getExceptionTypes() 方法
“方法类”的Java.lang.reflect.Method.getExceptionTypes()方法返回一个异常类型类对象数组,声明为由方法对象抛出以处理方法内部的异常。使用 throwed 子句的方法处理的所有异常都作为使用此方法的 Class 对象数组返回。如果应用此方法的方法在其 throws 子句中未声明异常,则此方法返回长度为 0 的数组。
句法:
public Class>[] getExceptionTypes()
返回值:该方法返回一个由this Method对象使用 throw 子句声明的异常类的数组
下面的程序说明了 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 对象是否抛出了某个定义的异常。如果是,则打印 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 Java