Exception in Inheritance

Case 1 : Parent class throwing any exception(checked/unchecked)


a) Exception thrown in the parent class’s method is checked type.

If the exception is thrown by the parent’s class method then child class’s overridden method may not be required to throw the exception (not mandatory but it can throw)

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

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


b) Exception thrown in the parent class’s method is unchecked type.

If the exception is thrown by the parent’s class method then child class’s overridden method may not be required to throw the exception(not mandatory but it can throw)

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

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

Ground Rule :
If exception(checked/unchecked) is thrown in the parent class’s method then child class’s overridden method is not forced to through an exception. However it can through the exception if it wants(rules will apply).


Case 2 : Child class throwing checked exception


Child is throwing any checked exception but parent is not throwing any exception

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

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


Will the above program executes successfully ?

It will fail at compile time.

The reason is, It’s throwing checked exception.

Ground Rule :
If the child class is throwing any checked exception then parent must also through same exception (OR) any of its parent exception otherwise compilation fails.


Case 3 : Child class throwing unchecked exception

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

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


Compilation success ?

Yes , Because it is throwing run time exception

Ground Rule :
If the child is throwing any unchecked exception then parent need not to throw exception.


Let’s apply above Ground Rules for the below programs


1) Think and apply the Ground Rules

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

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


Do you think above program compile successfully ?

Yes 🙂 Because Ground Rules are applied

1) If parent is throwing any exception then child may not be required to throw exception(but it can throw)    
      
    satisfied

2) If the child is throwing any unchecked exception then parent need not to throw exception(but it can throw)  
        
    satisfied



2) Now see the below program(just revrse of above program)

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

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


Do you think above program compile successfully ?

Yes 🙂 it will compile

Think and apply the common Ground rules

1) If parent is throwing any exception then child may not be required to throw exception(but it can throw)   
      
            Satisfied

2) If the child is throwing any unchecked exception then parent need not to throw exception(but it can throw)

          Satisfied

3) If child is throwing any checked exception then parent should throw same exception or its parent exception

           Not satisfied


So, Above program fails in compilation because child is throwing checked exception


3) Do you think below program compiles successfully ?

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

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


No 🙂 above program will not compile successfully, Because of below Ground Rules

1) If the child is throwing any checked exception then parent must throw only checked exception

a) Child should throw same exception as parent (or) 

b) Child should throw any subclass exception of the parent.

             Not Satidfied



So, Below program is the best example for above Ground Rules

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

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

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