Java Exception handling with method overriding

If the superclass method does not declare an exception then the subclass overridden method cannot declare the checked exception but it can declare an unchecked exception.

If the superclass method declares an exception then the subclass overridden method can declare the same exception, subclass exception, or no exception but cannot declare the parent exception of the exception thrown by the superclass method.

 

Example: If the superclass method does not declare an exception then the subclass overridden method cannot declare the checked exception.

 

import java.io.IOException;

class SuperClass{
	public void display(){
		System.out.println("Super class.");
	}
}

class SubClass extends SuperClass {
	//Compile time error here.
	public void display() throws IOException{
		System.out.println("Sub class.");
	}
}
public class ExceptionHandlingInOverriding1 {
	public static void main(String args[]){
		//Creating subclass object.
		SuperClass obj = new SubClass();
		
		//method call.
		obj.display();
	}
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:  
Exception IOException is not compatible with 
throws clause in SuperClass.display()

 

Example: If the superclass method does not declare an exception then the subclass overridden method can declare an unchecked exception.

 

class SuperClass{
	public void display(){
		System.out.println("Super class.");
	}
}

class SubClass extends SuperClass {
	//No Compile time error here.
	public void display() throws ArithmeticException{
		System.out.println("Sub class.");
	}
}
public class ExceptionHandlingInOverriding2 {
	public static void main(String args[]){
		//Creating subclass object.
		SuperClass obj = new SubClass();
		
		//method call.
		obj.display();
	}
}

Output

Sub class.

 

Example: If the superclass method declares an exception then the subclass overridden method can declare the same exception.

 

class SuperClass{
	public void display()throws ArithmeticException{
		System.out.println("Super class.");
	}
}

class SubClass extends SuperClass {
	//can declare same exception.
	public void display() throws ArithmeticException{
		System.out.println("Sub class.");
	}
}

public class ExceptionHandlingInOverriding3 {
	public static void main(String args[]){
		//Creating subclass object.
		SuperClass obj = new SubClass();
		
		//method call.
		try {
			obj.display();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

Output

Sub class.

 

Example: If the superclass method declares an exception then the subclass overridden method can declare a subclass exception.

 

class SuperClass{
	public void display()throws Exception{
		System.out.println("Super class.");
	}
}

class SubClass extends SuperClass {
	//can declare same exception.
	public void display() throws ArithmeticException{
		System.out.println("Sub class.");
	}
}

public class ExceptionHandlingInOverriding4 {
	public static void main(String args[]){
		//Creating subclass object.
		SuperClass obj = new SubClass();
		
		//method call.
		try {
			obj.display();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

Output

Sub class.

 

Example: If the superclass method declares an exception then the subclass overridden method can declare no exception.

 


class SuperClass{
	public void display()throws Exception{
		System.out.println("Super class.");
	}
}

class SubClass extends SuperClass {
	//can declare with no exception.
	public void display(){
		System.out.println("Sub class.");
	}
}

public class ExceptionHandlingInOverriding5 {
	public static void main(String args[]){
		//Creating subclass object.
		SuperClass obj = new SubClass();
		
		//method call.
		try {
			obj.display();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

Output

Sub class.

 

Example: If the superclass method declares an exception then the subclass overridden method cannot declare a parent exception.

 

class SuperClass{
	public void display()throws ArithmeticException{
		System.out.println("Super class.");
	}
}

class SubClass extends SuperClass {
	//Compile time error here.
	public void display() throws Exception{
		System.out.println("Sub class.");
	}
}

public class ExceptionHandlingInOverriding6 {
	public static void main(String args[]){
		//Creating subclass object.
		SuperClass obj = new SubClass();
		
		//method call.
		try {
			obj.display();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
}

Output

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
Exception Exception is not compatible with 
throws clause in SuperClass.display()