Java - TypeNotPresentException



The Java TypeNotPresentException is thrown when a class is not present in the classpath. The TypeNotPresentException is a runtime exception that is thrown when a class is not present in the classpath.

It is a unchecked exception that extends the RuntimeException class. The TypeNotPresentException is a subclass of ClassNotFoundException.

It is a subclass of java.lang.reflect.MalformedParameterizedTypeException.

Following are the reason when JVM throws a TypeNotPresentException in Java:

  • When a class is not present in the classpath, JVM throws a TypeNotPresentException.

Constructors of TypeNotPresentException

There are two constructors of TypeNotPresentException class:

  • TypeNotPresentException(String typeName): This constructor is used to create a TypeNotPresentException object with the name of the class that is not present in the classpath.
  • TypeNotPresentException(String typeName, Throwable cause): This constructor is used to create a TypeNotPresentException object with the name of the class that is not present in the classpath and the cause of the exception.

Methods of TypeNotPresentException

There are some methods of TypeNotPresentException class:

Method Description
getMessage() It is used to return the message of the exception.
toString() It is used to return the detail message string of the exception.
printStackTrace() It is used to print the stack trace of the exception.
getTypeName() It is used to return the name of the class that is not present in the classpath.
getCause() It is used to return the cause of the exception.
initCause(Throwable cause) It is used to initialize the cause of the exception.
fillInStackTrace() It is used to fill in the execution stack trace.
printStackTrace(PrintStream s) It is used to print the stack trace of the exception to the specified print stream.

Hierarchy of TypeNotPresentException

Given below is the class hierarchy of TypeNotPresentException:

java.lang.Object
   ↳ java.lang.Throwable
       ↳ java.lang.Exception
           ↳ java.lang.RuntimeException
               ↳ java.lang.reflect.MalformedParameterizedTypeException
                   ↳ java.lang.reflect.TypeNotPresentException

Example of TypeNotPresentException

In this example, we are trying to load a class that is not present in the classpath, so JVM will throw a TypeNotPresentException.

public class TypeNotPresentExceptionExample {
   public static void main(String[] args) {
      throw new TypeNotPresentException("com.example.NonExistentClass", new ClassNotFoundException("Class not found"));
   }
}

Output

Following is the output of the above code:

Exception in thread "main" java.lang.TypeNotPresentException: Type com.example.NonExistentClass not present
	at TypeNotPresentExceptionExample.main(TypeNotPresentExceptionExample.java:3)
Caused by: java.lang.Cla

As you can see in the output, JVM throws a TypeNotPresentException because we are trying to load a class that is not present in the classpath.

Handling TypeNotPresentException

In this example, we are trying to load a class that is not present in the classpath, so JVM will throw a TypeNotPresentException. We are handling this exception using try-catch block.

public class TypeNotPresentExceptionExample {
   public static void main(String[] args) {
      try {
         Class.forName("com.example.NonExistentClass"); 
      } catch (ClassNotFoundException e) {
            System.out.println("TypeNotPresentException: Class not found in the classpath!");
        } catch (TypeNotPresentException e) {
            System.out.println("TypeNotPresentException: " + e.getMessage());
        } catch (Throwable e) {
            System.out.println("Throwable: " + e.getMessage());
      }
   }
}

Output

Following is the output of the above code:

TypeNotPresentException: Class not found in the classpath!

As you can see in the output, we are handling the TypeNotPresentException using try-catch block.

How to avoid TypeNotPresentException?

There are some ways to avoid TypeNotPresentException in Java:

  • Always check the classpath before loading a class.
  • Use try-catch block while loading a class.
  • Use the ClassLoader class to load a class dynamically
  • Also, can use Class.forName() method to load a class dynamically
  • Use the ClassLoader.getSystemClassLoader() method to get the system class loader.

By following the above ways, you can avoid TypeNotPresentException in Java.

Example of avoiding TypeNotPresentException using ClassLoader

Let's see an example to avoid TypeNotPresentException:

public class TypeNotPresentAvoidanceExample {
   public static void main(String[] args) {
      String className = "com.example.NonExistentClass"; // Class that might not exist

      try {
         Class<?> clazz = ClassLoader.getSystemClassLoader().loadClass(className);
         System.out.println("Class loaded successfully: " + clazz.getName());
      } catch (ClassNotFoundException e) {
         System.out.println("Class not found, handling gracefully instead of throwing TypeNotPresentException.");
      }
   }
}

Output

Following is the output of the above code:

Class not found, handling gracefully instead of throwing TypeNotPresentException.

As you can see in the output, we are handling the TypeNotPresentException using ClassLoader.

java_lang_exceptions.htm
Advertisements