Java throws with method overriding

In exception handling, there are certain rules to follow for dealing with method overriding using throws clause. Basically, rules are to used to avoid ambiguity for the Java compiler. The rules are as follows:         

Rule 1 >> If the method of superclass does not declare any exception: If the method of superclass does not declare any exception, the overridden method in subclass cannot declare the checked exception but it can declare unchecked exception.

So, we have to consider two scenarios for Rule 1

1A) overridden method in subclass declares checked exception and

1B) overridden method in subclass declares unchecked exception.

Rule 2 >> If the method of superclass declares an exception: If the method of superclass declares an exception, the overridden method in subclass can declare the same exception, subclass exception or no exception but cannot declare the superclass of this exception.

Therefore, we have to consider four scenarios for Rule 2

2A) overridden method in subclass declares same exception,

2B) overridden method in subclass declares subclass exception,

2C) overridden method in subclass declares no exception and

2D) overridden method in subclass declares superclass exception.

 
The rules are demonstrated using coding examples. See below:

[1A] Method in superclass does not declare any exception, overridden method in subclass declares checked exception:~

Except08a.java

import java.io.IOException;

class Super {
void print() { //declares no exception
System.out.println("from superclass");
}
}

class Sub extends Super {
void print() throws IOException { //declares checked exception
System.out.println("from subclass");
}
}

public class Except08a {

public static void main(String[] args) {
Super s;
s = new Super();
s.print();
s = new Sub();
s.print();
}
}

Output:

Compilation Error

 

[1B] Method in superclass does not declare any exception, overridden method in subclass declares unchecked exception:~

Except08b.java

class Super {
void print() { //declares no exception
System.out.println("from superclass");
}
}

class Sub extends Super {
void print() throws NullPointerException { //declares unchecked exception
System.out.println("from subclass");
}
}

public class Except08b {

public static void main(String[] args) {
Super s;
s = new Super();
s.print();
s = new Sub();
s.print();
}
}

Output:

from superclass
from subclass

 

[2A] Method in superclass declares an exception, overridden method in subclass declares same exception:~

Except08c.java

import java.io.IOException;

class Super {
void print() throws IOException { //declares an exception
System.out.println("from superclass");
}
}

class Sub extends Super {
void print() throws IOException { //declares same exception as in superclass
System.out.println("from subclass");
}
}

public class Except08c {

public static void main(String[] args) throws Exception {
Super s;
s = new Super();
s.print();
s = new Sub();
s.print();
}
}

Output:

from superclass
from subclass

 

[2B] Method in superclass declares an exception, overridden method in subclass declares subclass exception:~

Except08d.java

import java.io.IOException;

class Super {
void print() throws IOException { //declares an exception
System.out.println("from superclass");
}
}

class Sub extends Super {
void print() throws FileNotFoundException { //declares subclass exception
System.out.println("from subclass");
}
}

public class Except08d {

public static void main(String[] args) throws Exception {
Super s;
s = new Super();
s.print();
s = new Sub();
s.print();
}
}

Output:

from superclass
from subclass

 

[2C] Method in superclass declares an exception, overridden method in subclass declares no exception:~

Except08e.java

import java.io.IOException;

class Super {
void print() throws IOException { //declares an exception
System.out.println("from superclass");
}
}

class Sub extends Super {
void print() { //declares no exception
System.out.println("from subclass");
}
}

public class Except08e {

public static void main(String[] args) throws Exception {
Super s;
s = new Super();
s.print();
s = new Sub();
s.print();
}
}

Output:

from superclass
from subclass

 

[2D] Method in superclass declares an exception, overridden method in subclass declares superclass exception:~

Except08f.java

import java.io.IOException;

class Super {
void print() throws FileNotFoundException { //declares an exception
System.out.println("from superclass");
}
}

class Sub extends Super {
void print() throws IOException { //declares superclass exception
System.out.println("from subclass");
}
}

public class Except08f {

public static void main(String[] args) throws Exception {
Super s;
s = new Super();
s.print();
s = new Sub();
s.print();
}
}

Output:

Compilation Error