Superclass of all exception classes

The java.lang.Throwable is the superclass of all exception classes in java.

Hierarchy Exception

Hierarchy Error

Example

public class Main {
 
    public static void main(String[] args) {
       int num1= 30, num2 = 0; 
 
		// Try to divide by zero 
		try { 
			int result = num1 / num2; 
		} 
		catch (Throwable e) { 
		    System.out.println("Exceptionn class: " + e.getClass().getName());
			e.printStackTrace(); 
		} 
    }
}

Output

Exceptionn class: java.lang.ArithmeticException
java.lang.ArithmeticException: / by zero
        at Main.main(Main.java:8)

Java interview questions on Exception Handling

Content Protection by DMCA.com
Please Share