Throwable, Exception, RuntimeException, Error

Throwable, Exception, RuntimeException, Error

User-defined exception handling in Java programming is completely dependent on the Throwable class, which is a custom-generated exception hierarchy root structure. All types of custom-generated user exceptions and program errors in Java programming are elements or features of the Throwable subclass. The Throwable class in Java is divided into two main branches, the first defined as the Exception and the second as the Error classes.

Throwable, Exception, RuntimeException, Error

So, let’s take a closer look at the Throwable class and their relationship in Java.

Throwable Class in Java.

The built-in Throwable class in the Java library is the superclass for all user-defined custom errors and exceptions in Java programming. Here, the Throwable class is defined into two primary subclasses, known as the Error and Exception classes.

Java Throwable Class Hierarchy Structure.

  • Remember, the Throwable class in Java is defined as the parent class of both Error and Exception.
  • All other Error and Exception subclasses, both checked and unchecked, finally extend the Throwable parent class.

Main methods of the Java Throwable class.

  • getMessage() – Provides detailed message information about the current program-generated exception or user-defined error.
  • printStackTrace() – Prints stack trace information in the current program, which is useful for debugging current program exceptions or errors.
  • toString() – Provides a string description of the user-defined program exception or error message in the current Java program.

Java Throwable class Exception Illustration.

public class ThrowableExceptionIllustration {

public static void main(String[] args) {

try {

throw new Exception(“Custom user generated throwable Exception”);

} catch (Throwable t) {

System.out.println(t.toString());

t.printStackTrace();

}

}

}

Exception Class in Java.

In Java programming, Exception is defined as a subclass of the Throwable parent class. It represents exceptional conditions in a Java program that must be caught or handled if necessary. The Exception class is defined as the superclass for all checked exceptions, and it can have multiple subclasses.

Types of the Java Exception Class.

Checked Exception – Checked exceptions in Java programs are program exceptions that the Java compiler indicates should be handled or managed in a clear order by the Java user. These are subclasses of user-defined exceptions in Java, but are not RuntimeExceptions.

  • Example – IOException, SQLException, ClassNotFoundException, etc.

Unchecked Exception – Unchecked exceptions in Java programs are program exceptions that do not need to be manually handled or managed by the Java programmer. These are subclasses of RuntimeException in Java programs, and generally represent a bug in the existing programming, or a logic or condition that is more difficult to recover from later.

  • Example – NullPointerException, IndexOutOfBoundsException, IllegalArgumentException, etc.

Example of a checked exception/IOException in Java.

import java.io.*;

public class CheckedExceptionIllustration {

public static void main(String[] args) {

try {

FileReader reader = new FileReader(“notexist.txt”);

} catch (IOException e) {

System.out.println(“here we can caught IOException – ” + e.getMessage());

}

}

}

Example of an unchecked exception/NullPointerException in Java.

public class UncheckedExceptionIllustration {

public static void main(String[] args) {

String str1 = null;

try {

System.out.println(str1.length()); // here This will throw a NullPointerException in active program

} catch (NullPointerException e) {

System.out.println(“here we can Caught NullPointerException – ” + e.getMessage());

}

}

}

RuntimeException class in Java.

In Java programming, RuntimeException is defined as a subclass of Java Exception and is a built-in part of the collection of unchecked exceptions in a program. Remember, user-defined unchecked exceptions in Java programs do not need to be handled or managed in a clear order in the program source code, and are generated due to common programming issues or mistakes that are more complex to find or detect in a program. For example, null pointer dereference, array index out of bounds, etc.

Unchecked exceptions – Unchecked exceptions in Java programs are generated or developed during program runtime. In Java, these unchecked exceptions do not need to be declared in the throws clause of a method or caught using a try-catch block.

Common Java RuntimeException subclass elements.

  • NullPointerException.
  • ArithmeticException.
  • IndexOutOfBoundsException.
  • IllegalArgumentException.
  • IllegalStateException.

Example of a Java RuntimeException.

public class RuntimeExceptionIllustration {

public static void main(String[] args) {

int[] array = {9, 4, 7, 8};

try {

System.out.println(array[9]); // here This will throw an ArrayIndexOutOfBoundsException error in program

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println(“here runtime program exception Caught – ” + e.getMessage());

}

}

}

Here in this example.

  • Here, the runtime exception ArrayIndexOutOfBoundsException indicates a runtime program exception and does not need to be caught or declared explicitly in the current program.

Error Class in Java.

The Error exception in Java is defined as a subclass of the Throwable parent class. Unlike program exceptions, errors generally indicate complex program issues that do not need to be handled or managed by the Java application. Program errors in any Java program are typically thrown automatically by the Java Virtual Machine (JVM), the Java compiler, and represent complex problems in the current Java program from which the program cannot recover.

Types of Java exception errors.

  • OutOfMemoryError – This error occurs in a Java program when the JVM (Java Virtual Machine) runs out of memory.
  • StackOverflowError – This error occurs in a Java program when the call stack in a Java program overflows, such as due to infinite recursion, etc.
  • VirtualMachineError – This error occurs in a Java program when a serious, complex internal error occurs in the JVM.

Errors in Java programs are unchecked, meaning that the Java user does not need to manually manage or handle them using try-catch blocks in the current program. Handling errors in Java programs is generally not recommended because Java program errors indicate complex program issues, logic, or conditions that cannot be resolved by normal exception handling in a Java program.

Example of a Java exception error.

public class JavaErrorIllustration {

public static void main(String[] args) {

try {

recursionCall();

} catch (StackOverflowError e) {

System.out.println(“here it can Caught StackOverflowError in program – ” + e.getMessage());

}

}

// here This will cause a StackOverflowError due to infinite recursion program situation

public static void recursionCall() {

recursionCall();

}

}

In this example.

  • In this program, the recursive function method recursionCall() displays a StackOverflowError because the recursion function defined in the Java program will run forever. This is why the JVM displays this error, and this error is caught in a user-defined catch block.

Leave a Reply