Throwable Class in Java with Examples

Last Updated : 30 Jun, 2026

The Throwable class is the root class of Java's exception hierarchy. It represents all objects that can be thrown and caught during program execution. Both Exception and Error classes inherit from Throwable, making it the base class for handling runtime errors and exceptional conditions..

  • Located in the java.lang package.
  • Superclass of both Exception and Error.
  • Implements the Serializable interface.
Java
class Main {
    public static void main(String[] args) {

        try {
            throw new Exception("Something went wrong!");
        }
        catch (Throwable t) {
            System.out.println("Message: " + t.getMessage());
            System.out.println("Exception: " + t);
        }
    }
}

Output
Message: Something went wrong!
Exception: java.lang.Exception: Something went wrong!

Explanation : In this example, an Exception object is thrown and caught as a Throwable. The getMessage() method displays the exception message, while toString() (called automatically) prints the exception type along with its message. This demonstrates how the Throwable class acts as the common parent of all exceptions and errors in Java.

Syntax

public class Throwable extends Object implements Serializable

Types of Throwable

The Throwable class has two direct subclasses:

object

Exception

The Exception class represents conditions that an application can handle. These exceptions can usually be caught and recovered from during program execution.

  • Can be handled using try-catch.
  • Includes checked and unchecked exceptions.

Error

The Error class represents serious problems that usually occur due to the Java Virtual Machine (JVM). These errors are generally not intended to be handled by applications.

  • Caused by JVM or system failures.
  • Normally not handled using try-catch.

Common Constructor

ConstructorDescription
Throwable()Creates a throwable with no detail message.
Throwable(String message)Creates a throwable with a specified message.
Throwable(String message, Throwable cause)Creates a throwable with a message and cause.
Throwable(Throwable cause)Creates a throwable with the specified cause.

Common Methods

MethodDescription
getMessage()Returns the exception message.
getCause()Returns the cause of the exception.
printStackTrace()Prints the complete stack trace.
toString()Returns the exception class and message.
getStackTrace()Returns stack trace elements.
initCause()Sets the cause of the exception.

Below program demonstrates the toString() method of Throwable class:

Java
import java.io.*;

class GFG {

    // Main Method
    public static void main(String[] args)
        throws Exception
    {

        try {

            testException();
        }

        catch (Throwable e) {

            // Print using tostring()
            System.out.println("Exception: "
                               + e.toString());
        }
    }

    // Method which throws Exception
    public static void testException()
        throws Exception
    {

        throw new Exception(
            "New Exception Thrown");
    }
}

Output
Exception:
 java.lang.Exception:
 New Exception Thrown

Explanation : In this example, the testException() method explicitly throws a new Exception with the message "New Exception Thrown". The main() method calls this method inside a try block. Since Exception is a subclass of Throwable, it is caught by the catch (Throwable e) block. Finally, the toString() method is used to display the exception's class name along with its message.

Below program demonstrate the getMessage() method of java.lang.Throwable Class: 

Java
import java.io.*;

class GFG {

    // Main Method
    public static void main(String[] args)
        throws Exception
    {

        try {

            // Divide the numbers
            divide(2, 0);
        }

        catch (ArithmeticException e) {

            System.out.println(
                "Message String = "
                + e.getMessage());
        }
    }

    // Method which divides two numbers
    public static void divide(int a, int b)
        throws ArithmeticException
    {

        int c = a / b;
        System.out.println("Result:" + c);
    }
}

Output
Message String = / by zero

Explanation: In this example, the divide() method attempts to divide 2 by 0, which causes an ArithmeticException. The exception is caught in the catch block, and the getMessage() method is used to display the exception message. This demonstrates how the Throwable class (through its subclass ArithmeticException) provides descriptive error messages using the getMessage() method.

Comment