Java-Throwable Class and Generic Exception Catch

Throwable Class

All the exceptions are derived from the class Throwable. Thus, Throwable is a superclass of all other exception class. There are two subclasses of Throwable class. They are Exception and Error. The Exception class has a many subclasses, such as RunTimeException, IOException and etc….

 

Generic Exception catch

A catch statement that uses a superclass will catch exceptions of that type plus any of its subclasses. For example, in the following program, the catch statement will handle all Exception-based errors.

Program

Program Source

class Javaapp {

    public static void main(String[] args) {
        
        try{
  
            int a=1/0;
            
        }catch(Exception ex)
        {
            System.out.println("Exception : "+ex.getClass().getName());
        }
        System.out.println("Execution Complete");
    }
}

Leave a Comment