Exception in inheritance continued

Based on previous post ,observe the below program

1
2
3
4
5
6
7
8
9
10
11
12
13
class Parent{
    
    void method1() throws SQLException  {
        
    }
}
 
class Child extends Parent{
    
    void method1() throws RuntimeException {
        
    }
}
class Parent{
	
	void method1() throws SQLException  {
		
	}
}

class Child extends Parent{
	
	void method1() throws RuntimeException {
		
	}
}

Will it compile successfully ?
Yes yes yes

Apply previous post’s ground rules

1st ground rule – if parent is throwing any exception then child may not be required to throw exception(but it can throw) ->satisfied

2nd ground rule – if the child is throwing any unchecked exception then parent need not to throw exception(but it can throw) ->satisfied
Hence above program compiles successfully.

Now see the below program(just revrse of above program)
class Parent{

void method1() throws RuntimeException {

}
}

class Child extends Parent{

void method1() throws SQLException {

}
}
will it compiles successfully ?

Yes ,are you sure ? yes, are you sure ? yes, think

Lets apply the common ground rules

1st ground rule – if parent is throwing any exception then child may not be required to throw exception(but it can throw) -> satisfied

2nd ground rule – if the child is throwing any unchecked exception then parent need not to throw exception(but it can throw) -> since it is pointing out child should throw unchecked exception to have this ground rule
But in above program child is throwing checked exception, oh god then what happens ?

Above program compilation fails fails fails 100% yes it fails.

Ground rule : if the child is throwing any checked exception then parent must throw only checked exception.

Then will the below program compiles successfully?
class Parent{

void method1() throws IOException {

}
}

class Child extends Parent{

void method1() throws SQLException {

}
}

No the rule is if child is throwing any checked exception then parent must throw a)same checked exception or b)super type of that checked exception

So below program is the best example of this
class Parent{

void method1() throws SQLException {

}
}

class Child extends Parent{

void method1() throws SQLDataException {

}
}

Ground rule : : if the child is throwing any checked exception then parent must throw only checked exception and it should satisfy the above condition
a)child should throw same exception as parent
or
b)child should throw any subclass exception of the parent.

Practice all the ground rules , then you will be good in Exception in inheritance.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on