Sunday, March 22, 2015

How to handle Exceptions when doing method overriding

There are several possible scenarios when talk about this topic. Let's look at them one by one

1 . When the super class method does not declare exceptions.
     Three possible scenarios comes under this.
 
   (i) . Child class overriding method may not declare any exception.
   (ii). Child class overriding method should not declare any checked exception.Below example                 illustrates  this situation.

public class SuperClass1 {
    
    public void printName(){
        System.out.println("..");
        
    }

}


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;


public class ChildClass1 extends SuperClass1 {
    
    
    @Override
    public void printName() throws FileNotFoundException {
        
        File f= new File("C:\\Users\\Hasitha\\Desktop\\syn");
        
        
    }

    
}

Here I get a compile time error since I have declared a checked exception.

(iii).Child class overriding method can declare any unchecked exception.Below example                     illustrates  this situation.

public class SuperClass2 {
    
    public int devision(){
        int num=10;
        int ans = 0;
        for (int i = 5; i >=1 ; i--) {
            ans=num/i;
        }
        return ans;
    }

}


public class ChildClass2 extends SuperClass2 {
    
    @Override
    public int devision() throws ArithmeticException {
            
            int num=10;
            int ans = 0;
            
            for (int i = 5; i >=0 ; i--) {
                ans=num/i;
            }
            
            return ans;
        
        
    }
    
    public static void main(String[] args) {
        ChildClass2 c= new ChildClass2();
        c.devision();
    }

}

Here I have declared an unchecked exception with child class overriding method.


2.When super class  method declare exceptions.(Checked or Unchecked Exceptions)
 There are  three possible scenarios comes under here also.

(i). Sub class overriding method may not declare any exception.
   Below example illustrates this situation. 

public class SuperClass3 {
    
    public void testArray() throws ArrayIndexOutOfBoundsException{
        int num[]={1,2,3,4,5};
        
        for (int i=0;i<8;i++) {
            System.out.println(num[i]);
        }
    }
    
    
}


public class ChildClass3 extends SuperClass3 {
    
    @Override
    public void testArray()  {
        
        int num[]={1,2,3,4,5};
        
        for (int i=0;i<8;i++) {
            System.out.println(num[i]);
        }
        
    }

}

Here I have declared a checked exception in the super class . But when overriding the super class method I have not declare that exception.

(ii). Sub class overriding method may  declare the same exception.
   Below example illustrates this situation. 

  

public class SuperClass3 {
    
    public void testArray() throws ArrayIndexOutOfBoundsException{
        int num[]={1,2,3,4,5};
        
        for (int i=0;i<8;i++) {
            System.out.println(num[i]);
        }
    }
    
    
}



public class ChildClass3 extends SuperClass3 {
    
    @Override
    public void testArray() throws ArrayIndexOutOfBoundsException  {
        
        int num[]={1,2,3,4,5};
        
        for (int i=0;i<8;i++) {
            System.out.println(num[i]);
        }
        
    }

}


(iii).Sub class overriding method may  declare a more specific exception when compared with the super class exception. As an example if super class have declared an IOException child class can declare  a FileNotFoundException instead of IOException. But keep in mind that child class overriding method cannot declare more general exception  when compare with the super class method.

Below Example illustrates how child class overriding method can declare a more specific exception.


import java.io.File;
import java.io.IOException;


public class SuperClass4 {
    
    public void handleMyFile() throws IOException{
        File f= new File("C:\\Users\\Hasitha\\Desktop\\syn");
        
    }

}


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;


public class ChildClass4 extends SuperClass4 {
    
    
        
public void handleMyFile() throws FileNotFoundException{
    File f= new File("C:\\Users\\Hasitha\\Desktop\\syn");
            
}
    
    

}




No comments:

Post a Comment