Method Class | getExceptionTypes() Method in Java (original) (raw)
Last Updated : 11 Apr, 2023
The java.lang.reflect.Method.getExceptionTypes() method of “Method class” returns an array of Exception Type Class Objects declared to be thrown by the method object to handle exception inside the method. All the exceptions handled by method using thrown clause, are returned as array of Class 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. Syntax:
public Class<?>[] getExceptionTypes()
Return Value: This method returns an array of the exception class declared using thrown clause by the this Method object Below programs illustrates getExceptionTypes() method of Method class: Example 1: Print all Exceptions
Java
import
java.lang.reflect.Method;
public
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` try
{
`` Class classobj = demoClass.
class
;
`` Method[] methods = classobj.getMethods();
`` for
(Method method : methods) {
`` if
(method.getName().equals("setValue")
`` || method.getName().equals("getValue")) {
`` Class[] exceptions = method.getExceptionTypes();
`` 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();
`` }
`` }
}
class
demoClass {
`` public
void
setValue(String value)
`` throws
ClassNotFoundException,
`` ArrayIndexOutOfBoundsException,
`` ArithmeticException
`` {
`` }
`` public
String getValue(String value)
`` {
`` return
value;
`` }
}
Output:
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
Example 2: Check whether certain defined Exception is thrown by Method object or not. If yes, then print true, otherwise print false.
Java
import
java.lang.reflect.Method;
class
GFGSampleClass {
`` String value;
`` public
void
setValue(String value)
`` throws
ClassNotFoundException,
`` ArrayIndexOutOfBoundsException,
`` ArithmeticException
`` {
`` this
.value = value;
`` }
}
public
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` try
{
`` Class classobj = GFGSampleClass.
class
;
`` Method[] methods = classobj.getMethods();
`` for
(Method method : methods) {
`` if
(method.getName().equals("setValue")) {
`` Class exceptionObj = IndexOutOfBoundsException.
class
;
`` boolean
response = isCertainExceptionIsThrown(method,
`` exceptionObj);
`` System.out.println("IndexOutOfBoundsException is "
`` + "thrown by setValue(): " + response);
`` }
`` }
`` }
`` catch
(Exception e) {
`` e.printStackTrace();
`` }
`` }
`` private
static
boolean
`` isCertainExceptionIsThrown(Method method, Class<?> exceptionName)
`` {
`` Class exceptions[] = method.getExceptionTypes();
`` for
(
int
i =
0
; i < exceptions.length; i++) {
`` if
(exceptions[i] == exceptionName) {
`` return
true
;
`` }
`` }
`` return
false
;
`` }
}
Output:
IndexOutOfBoundsException is thrown by setValue(): false
Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getExceptionTypes–