à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday, 8 October 2017

Exception Handling in JAVA 9


Exception Handling in JAVA 9

Exception Handling with Method Overriding in Java

RULE 1 :  If super class method does not declare any exception, then sub class overriden method cannot declare checked exception but it can declare unchecked exceptions : 

Example of Subclass overriden Method declaring Checked Exception :

import java.io.*;
class Super
{
 void show() { System.out.println("parent class"); }
}

public class Sub extends Super 
{
 void show() throws IOException		//Compile time error	  
  { System.out.println("parent class"); } 

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

As the method show() doesn't throws any exception while in Super class, hence its overriden version can also not throw any checked exception.

Example of Subclass overriden Method declaring Unchecked Exception. 

import java.io.*;
class Super
{
 void show(){ System.out.println("parent class"); }
}

public class Sub extends Super 
{
 void show() throws ArrayIndexOutOfBoundsException      //Correct
   { System.out.println("child class"); }

 public static void main(String[] args)
 {
  Super s=new Sub();
  s.show();
 }  
}
Output : child class

Because ArrayIndexOutOfBoundsException is an unchecked exception hence, overrided show() method can throw it.

RULE 2 : If Super class method throws an exception, then Subclass overriden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by Super class method :

It means, if Super class method throws object of NullPointerException class, then Subclass method can either throw same exception, or can throw no exception, but it can never throw object of Exception class (parent of NullPointerException class).

Example of Subclass overriden method with no Exception 

import java.io.*;
class Super
{
 void show() throws Exception 
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show()             		//Correct  	  
   { System.out.println("child class"); } 

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }  
}
Output : child class

Example of Subclass overriden method with same Exception.

import java.io.*;
class Super
{
 void show() throws Exception 
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show() throws Exception		//Correct  	  
   { System.out.println("child class"); } 

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }  
}
Output : child class

Example of Subclass overriden method with parent Exception.

import java.io.*;
class Super
{
 void show() throws ArithmeticException 
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show() throws Exception    //Compile time Error  	  
   { System.out.println("child class"); } 

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }  
}

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website