Saturday, July 4, 2026
HomeLanguagesJavaConstructor getGenericExceptionTypes() method in Java with Examples

Constructor getGenericExceptionTypes() method in Java with Examples

The getGenericExceptionTypes() method of java.lang.reflect.Constructor class is used to return the exception types present on this constructor object as an array. The returned array of objects represent the exceptions declared to be thrown by this constructor object. If this constructor declares no exceptions in its throws clause then this method returns an array of length 0.

Syntax:

public Type[] getGenericExceptionTypes()

Parameters: This method accepts nothing.

Return: This method returns an array of Types that represent the exception types thrown by the underlying executable.

Exception: This method throws following exceptions:

  • GenericSignatureFormatError: if the generic method signature does not conform to the format specified in The Javaâ„¢ Virtual Machine Specification.
  • TypeNotPresentException: if the underlying executable’s 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 illustrate getGenericExceptionTypes() method:
Program 1:




// Java program to illustrate
// getGenericExceptionTypes() method
  
import java.io.IOException;
import java.lang.reflect.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = shape.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons
            = classObj.getConstructors();
  
        // get array of GenericExceptionTypes
        Type[] exceptions
            = cons[0].getGenericExceptionTypes();
  
        // print length of GenericExceptionTypes array
        System.out.println("GenericExceptions : ");
  
        for (int i = 0; i < exceptions.length; i++)
            System.out.println(exceptions[i]);
    }
  
    // demo class
    public class shape {
  
        public shape() throws IOException,
                              ArithmeticException,
                              ClassCastException
        {
        }
    }
}


Output:

GenericExceptions : 
class java.io.IOException
class java.lang.ArithmeticException
class java.lang.ClassCastException

Program 2:




// Java program to illustrate
// getGenericExceptionTypes() method
  
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = String.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons
            = classObj.getConstructors();
  
        // get array of GenericExceptionTypes
        Type[] exceptions
            = cons[0].getGenericExceptionTypes();
  
        // print length of GenericExceptionTypes array
        System.out.println(
            "No of Generic Exception thrown : "
            + exceptions.length);
    }
}


Output:

No of Generic Exception thrown : 0

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getGenericExceptionTypes()

RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6900 POSTS0 COMMENTS
Nicole Veronica
12015 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6967 POSTS0 COMMENTS