Method Class | getGenericExceptionTypes() Method in Java
Last Updated :
10 Apr, 2023
The getGenericExceptionTypes() method of java.lang.reflectMethod class returns an array of Type objects representing Exceptions thrown by the method object to handle exception. All the exceptions handled by method using thrown clause are returned as array of Type objects using this method. This method returns an array of length 0, if the method, on which this method is applied, declares no exceptions in its throws clause. Example:
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,
}
Syntax:
public Type[] getGenericExceptionTypes()
Return Value: It returns an array of the exception types thrown by the this Method object Exception: This method throws following exceptions:
- GenericSignatureFormatError – if the generic method signature is not same as the format specified in The JVM Specification.
- TypeNotPresentException – if Exception Types specified by throws clause refers to a non-existent type declaration.
- MalformedParameterizedTypeException – if the underlying executable’s throws clause refers to a parameterized type that cannot be instantiated for any reason.
Below programs illustrates getGenericExceptionTypes() method of Method class: Program 1: Print all Exception Types thrown by a Method with the help of getGenericExceptionTypes()
Java
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GFG {
public static void main(String[] args)
{
try {
Class classobj = demoClass. class ;
Class[] parameterTypes = { String. class };
Method[] methods = classobj.getMethods();
for (Method m : methods) {
if (m.getName().equals("setValue")
|| m.getName().equals("getValue")) {
Type[] genericExceptions = m.getGenericExceptionTypes();
System.out.println("Generic Exception Thrown by Method: "
+ m.getName());
System.out.println("Generic Exception Type Array length: "
+ genericExceptions.length);
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();
}
}
}
class demoClass {
String value;
public void setValue(String value)
throws ClassNotFoundException,
ArrayIndexOutOfBoundsException,
ArithmeticException
{
this .value = value;
}
public String getValue()
{
return this .value;
}
}
|
Output:
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
Program 2: Check for a specific Exception
Java
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GFG {
class GFGSampleClass {
String value;
public void setValue(String value)
throws ClassNotFoundException,
ArrayIndexOutOfBoundsException,
ArithmeticException
{
this .value = value;
}
}
public static void main(String[] args)
{
try {
Class classobj = GFGSampleClass. class ;
Method[] methods = classobj.getMethods();
Method method = null ;
for (Method m : methods) {
if (m.getName().equals("setValue"))
method = m;
}
Type arithmeticExClassobj = ArithmeticException. class ;
boolean response = isCertainExceptionIsThrown(method,
arithmeticExClassobj);
System.out.println("ArithmeticException"
+ " is thrown by setValue(): " + response);
Type exceptionObj = IndexOutOfBoundsException. class ;
response = isCertainExceptionIsThrown(method,
exceptionObj);
System.out.println("IndexOutOfBoundsException"
+ " is thrown by setValue(): " + response);
}
catch (Exception e) {
e.printStackTrace();
}
}
private static boolean
isCertainExceptionIsThrown(Method method, Type exceptionName)
{
Type exceptions[] = method.getGenericExceptionTypes();
for ( int i = 0 ; i < exceptions.length; i++) {
if (exceptions[i] == exceptionName) {
return true ;
}
}
return false ;
}
}
|
Output:
ArithmeticException is thrown by setValue(): true
IndexOutOfBoundsException is thrown by setValue(): false
Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getGenericExceptionTypes–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...