Java中的方法类| getExceptionTypes()方法

这个 JAVA朗,反思 方法getExceptionTypes() “method class”的方法返回异常类型类对象的数组,这些对象声明由method对象抛出,以处理方法内部的异常。使用抛出子句的方法处理的所有异常,都将使用此方法作为类对象数组返回。如果应用此方法的方法在其throws子句中未声明任何异常,则此方法返回长度为0的数组。

null

语法:

public Class<?>[] getExceptionTypes()

返回值: 此方法返回This method对象使用抛出子句声明的异常类的数组

下面的程序演示了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: 检查某个已定义的异常是否由方法对象引发。如果是,则打印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–

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享