Difference between Throwable and Exception in Java

1. Introduction

In Java, error handling is an important part of any robust application. Java provides a hierarchy of classes to handle errors and exceptions. Throwable is at the top of this hierarchy, a class that all error and exception classes extend. An Exception is a subclass of Throwable intended for any recoverable condition that a reasonable application might want to catch.

2. Key Points

1. Throwable is the superclass of all errors and exceptions in Java.

2. Exception is a subclass of Throwable that represents conditions that a program should catch.

3. Errors are also subclasses of Throwable, representing serious problems that applications should not try to handle.

4. Exceptions can be checked or unchecked, with checked exceptions being those that must be either caught or declared in the method signature.

3. Differences

Throwable Exception
The superclass of all errors and exceptions in Java. A subclass of Throwable that indicates conditions that a reasonable application might want to catch.
Acts as the root class of Java's exception hierarchy.Primarily intended for situations where a program can recover or handle the problem.
Includes two major subclasses: Error and Exception.It is part of the "checked exceptions" category (except for RuntimeException and its subclasses, which are "unchecked exceptions").
Using Throwable in a catch block may catch both errors and exceptions, but this is generally discouraged unless specifically intended. Catching Error subclasses could mean catching system errors that an application cannot do much about.They are encouraged to be caught and handled in application code, with the aim of recovering from the exception or at least providing a graceful error-handling mechanism.
Example subclasses include OutOfMemoryError, StackOverflowError, VirtualMachineError, etc.Example subclasses include IOException, SQLException, ClassNotFoundException, RuntimeException, etc.

4. Example

// Example showing the use of Throwable and Exception
public class ExceptionHandlingExample {
    public static void main(String[] args) {
        try {
            // Step 1: Might throw an exception that is a subclass of Exception
            throwException();
        } catch (Exception e) {
            System.out.println("Caught an exception.");
        }

        try {
            // Step 2: Might throw an error that is not meant to be caught
            throwError();
        } catch (Throwable t) {
            System.out.println("Caught a throwable.");
        }
    }

    static void throwException() throws Exception {
        throw new Exception("This is an exception");
    }

    static void throwError() {
        throw new Error("This is an error");
    }
}

Output:

Caught an exception.
Caught a throwable.

Explanation:

1. throwException throws an Exception, which is caught in the corresponding catch block.

2. throwError throws an Error, which is not typically caught by applications but is caught here for demonstration purposes by catching Throwable.

5. When to use?

Use Exceptions when you are dealing with recoverable conditions and want your application to catch and handle the problem.

It is generally not a good practice to catch Throwable as it includes Error subclasses, which are not meant to be caught by applications (e.g., OutOfMemoryError, StackOverflowError).

Comments