Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavaMethod Class | getExceptionTypes() Method in Java

Method Class | getExceptionTypes() Method in Java

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




/*
* 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 their 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;
    }
}


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




// 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 their 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;
    }
}


Output:

IndexOutOfBoundsException is thrown by setValue(): false

Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getExceptionTypes–

RELATED ARTICLES

Most Popular

Recent Comments