Saturday 5 January 2019


         ExceptionHandling with MethodOverriding in Java



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)
package exceptionquestion;
import java.sql.SQLException;
public 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)
package exceptionquestion;
class Parent{
void method1() throws RuntimeException {
}
}
class Child extends Parent{
void method1() {
}
}
Note:-

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
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.
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
class Parent{
void method1() {
}
}
class Child extends Parent{
void method1() throws RuntimeException {
}
}
Compilation success ?
If the child is throwing any unchecked exception then parent need not to throw exception.
Yes , Because it is throwing run time exception

================================================================================================================================================

1) Think and apply the Rules

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

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





Do you think above program compile successfully ?

Yes


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

=============================================================================================================================================================================================


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

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



Do you think above program compile successfully ?

No



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



Above program fails in compilation because child is throwing checked exception




No  above program will not compile successfully.....



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 Rules

package exceptionquestion;

import java.sql.SQLDataException;
import java.sql.SQLException;

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

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




Do you think above program compile successfully ?

Yes