java.lang.reflectMethod類的getGenericExceptionTypes()方法返回一個Type對象數組,該對象表示方法對象拋出的用於處理異常的異常。使用此方法,由使用thrown子句的方法處理的所有異常均作為Type對象的數組返回。如果應用了此方法的方法在其throws子句中未聲明任何異常,則此方法返回長度為0的數組。
例:
Code: public class demo{ public void setValue(String value) throws ClassNotFoundException, ArrayIndexOutOfBoundsException, ArithmeticException {} } Explanation: In the above method when we going to apply getGenericExceptionTypes() method it is going to return array of the exception types. Array = { java.lang.ClassNotFoundException, java.lang.ArrayIndexOutOfBoundsException, java.lang.ArithmeticException, }
用法:
public Type[] getGenericExceptionTypes()
返回值:它返回此Method對象引發的異常類型的數組
異常:此方法引發以下異常:
- GenericSignatureFormatError-如果通用方法簽名與JVM規範中指定的格式不同。
- TypeNotPresentException-如果throws子句指定的異常類型引用了不存在的類型聲明。
- MalformedParameterizedTypeException-如果底層可執行文件的throws子句引用了由於某種原因而無法實例化的參數化類型。
以下示例程序旨在說明Method類的getGenericExceptionTypes()方法:
程序1:在getGenericExceptionTypes()的幫助下打印方法拋出的所有異常類型
/*
* Program Demonstrate how to apply
* getGenericExceptionTypes() method
*/
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GFG {
// Main method
public static void main(String[] args)
{
try {
// create class object
Class classobj = demoClass.class;
// create parameter type of string
Class[] parameterTypes = { String.class };
// get list of method objects
Method[] methods = classobj.getMethods();
// loop through all methods
for (Method m:methods) {
if (m.getName().equals("setValue")
|| m.getName().equals("getValue")) {
// apply getGenericExceptionTypes() method
Type[] genericExceptions = m.getGenericExceptionTypes();
// print exception Types thrown by method Object
System.out.println("Generic Exception Thrown by Method:"
+ m.getName());
System.out.println("Generic Exception Type Array length:"
+ genericExceptions.length);
// If method has Exception then print
if (genericExceptions.length > 0) {
System.out.println("Exception class object details:");
for (Type type:genericExceptions) {
System.out.println(type.getTypeName());
}
}
System.out.println();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// a simple class
class demoClass {
String value;
// throw some exception by method
public void setValue(String value)
throws ClassNotFoundException,
ArrayIndexOutOfBoundsException,
ArithmeticException
{
this.value = value;
}
// method throwing no exception
public String getValue()
{
return this.value;
}
}
輸出:
Generic Exception Thrown by Method:getValue Generic Exception Type Array length:0 Generic Exception Thrown by Method:setValue Generic Exception Type Array length:3 Exception class object details: java.lang.ClassNotFoundException java.lang.ArrayIndexOutOfBoundsException java.lang.ArithmeticException
程序2:檢查特定的異常
/*
* Program Demonstrate how to
* apply getGenericExceptionTypes() method
*/
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GFG {
// a simple class
class GFGSampleClass {
String value;
// throw some exception by method
public void setValue(String value)
throws ClassNotFoundException,
ArrayIndexOutOfBoundsException,
ArithmeticException
{
this.value = value;
}
}
// 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();
// get Method Object for setValue
Method method = null;
for (Method m:methods) {
if (m.getName().equals("setValue"))
method = m;
}
// check whether method throw
// ArithmeticException Exception
Type airthmeticExClassobj = ArithmeticException.class;
boolean response = isCertainExceptionIsThrown(method,
airthmeticExClassobj);
System.out.println("ArithmeticException"
+ " is thrown by setValue():" + response);
// check whether method throw
// IndexOutOfBoundsException Exception
Type exceptionObj = IndexOutOfBoundsException.class;
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, Type exceptionName)
{
// get all exception list using getGenericExceptionTypes()
Type exceptions[] = method.getGenericExceptionTypes();
for (int i = 0; i < exceptions.length; i++) {
// check exception thrown or not
if (exceptions[i] == exceptionName) {
return true;
}
}
return false;
}
}
輸出:
ArithmeticException is thrown by setValue():true IndexOutOfBoundsException is thrown by setValue():false
參考:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getGenericExceptionTypes-
相關用法
- Java Constructor getGenericExceptionTypes()用法及代碼示例
- Java Method類 getAnnotation()用法及代碼示例
- Java Method類 isVarArgs()用法及代碼示例
- Java Method類 getGenericParameterTypes()用法及代碼示例
- Java Method類 isBridge()用法及代碼示例
- Java Method類 getParameterCount()用法及代碼示例
- Java Method類 getName()用法及代碼示例
- Java Method類 toGenericString()用法及代碼示例
- Java Method類 hashCode()用法及代碼示例
- Java Method類 getParameterAnnotations()用法及代碼示例
- Java Method類 getParameterTypes()用法及代碼示例
- Java Method類 getAnnotatedReturnType()用法及代碼示例
- Java Method類 getReturnType()用法及代碼示例
- Java Method類 getModifiers()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Method Class | getGenericExceptionTypes() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。