Skip to main content Skip to docs navigation
TechSpiderTutorials

java.lang.Throwable class

On this page

Throwable Class in java

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

throwable class example

public class ThrowableExample {

public static void main(String[] args) {
    try {
        divideNumbers(10, 0);
    } catch (Throwable t) {
        System.out.println("Exception caught: " + t.getMessage());
    }
}

public static void divideNumbers(int dividend, int divisor) {
    if (divisor == 0) {
        throw new ArithmeticException("Division by zero");
    }
    int result = dividend / divisor;
    System.out.println("Result: " + result);
}
}