Monday, 11 March 2019


                                                   Exception Handling with Method Overriding


Case 1: If SuperClass doesn’t declare any exception and subclass declare checked exception
package exceptionwithinheritance;
public class ParentException {

public String printName(String s1)
{
return "Parent Exception="+s1;
}
}
package exceptionwithinheritance;
import java.io.IOException;
public class ChildException extends ParentException {
public String printName(String s1) throws IOException
{
return "Child Exception="+s1;
}
public static void main(String[] args) {
ParentException pe=new ChildException();
String ce = pe.printName("Java Technology");
System.out.println(ce);
}
}
o/p- compile time error
==============================================================================================================================
Case 2: If SuperClass doesn’t declare any exception and SubClass declare Unchecked exception
package exceptionwithinheritance;
public class ParentException {

public String printName(String s1)
{
return "Parent Exception="+s1;
}
}
package exceptionwithinheritance;
import java.io.IOException;
public class ChildException extends ParentException {
public String printName(String s1) throws ArithmeticException
{
return "Child Exception="+s1;
}
public static void main(String[] args) {
ParentException pe=new ChildException();
String ce = pe.printName("Java Technology");
System.out.println(ce);
}
}
o/p:---------------
Child Exception=Java Technology
================================================================================================================================
case:-3
If SuperClass declares an exception and SubClass declares exceptions other than the child exception of the SuperClass declared Exception
package exceptionwithinheritance;
public class ParentException {

public String printName(String s1) throws RuntimeException
{
return "Parent Exception="+s1;
}
}
package exceptionwithinheritance;
import java.io.IOException;
public class ChildException extends ParentException {
public String printName(String s1) throws Exception
{
return "Child Exception="+s1;
}
public static void main(String[] args) {
ParentException pe=new ChildException();
String ce = pe.printName("Java Technology");
System.out.println(ce);
}
}
o/p- compile time error
===============================================================================================================================
case:-4 If SuperClass declares an exception and SubClass declares an child exception of the SuperClass declared Exception.
package exceptionwithinheritance;
public class ParentException {

public String printName(String s1) throws RuntimeException
{
return "Parent Exception="+s1;
}
}
package exceptionwithinheritance;
import java.io.IOException;
public class ChildException extends ParentException {
public String printName(String s1) throws RuntimeException
{
return "Child Exception="+s1;
}
public static void main(String[] args) {
ParentException pe=new ChildException();
String ce = pe.printName("Java Technology");
System.out.println(ce);
}
}
o/p:----------------------------
Child Exception=Java Technology

No comments:

Post a Comment