Prog 1: If we use Checked Exception the program fails to compile
Here Student class has a method "tt" which throws an exception ClassNotFoundException which is a checked exception in java.
The Test class extends Student class and overrides the method "tt". This time instead of using try/catch I am trying to use throws keyword.
The result is a compile-time failure.
class Student{
public void tt(){
try {
throw new ClassNotFoundException();
}
catch(ClassNotFoundException e) {
}
}
}
class Test extends Student {
public void tt() throws ClassNotFoundException{
}
}
Prog 2: If we use Unchecked Exception the profile compiles and runs fine.
The same Student class has a method "tt" which throws an exception NullPointerException which is an unchecked exception in java.
The Test class extends Student class and overrides the method "tt". This time instead of using try/catch I am trying to use throws keyword.
The result is a successful run. The below program runs without any issues.
class Student{
public void tt(){
try {
throw new NullPointerException();
}
catch(NullPointerException e) {
}
}
}
class Test extends Student {
public void tt() throws NullPointerException{
}
}
Any leads is appreciated. Thank you!