-1

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!

Pranav Kumar
  • 83
  • 1
  • 8
  • 1
    Why? Because the language specification says that an overriding method cannot declare exceptions that the overridden method didn’t declare. And because when I have a reference to a `Student` object and call `tt`, no try-catch is needed. Now if the actual object was a `Test`, what should happen? It’d be a mess. – Ole V.V. Dec 26 '18 at 07:36

2 Answers2

2

Suppose you had this code:

Student s = new Test ();
s.tt ();

Since tt method of Student class has no throws clause, you are allowed to call s.tt() without handling any exceptions.

But if a sub-class of Student (Test in your case) overrides tt and throws a checked exception, the caller must handle that exception. But the caller doesn't know they have to handle it, since the base class has no throws clause. Therefore the sub-class is not allowed to add throws clauses of checked exceptions to the methods it overrides.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    For such an obvious duplicate, wouldn’t it be better to close than to provide a new answer? – Ole V.V. Dec 26 '18 at 07:39
  • Hi Eran, Thanks for the prompt reply. But what would be the reason that it works with unchecked/Runtime Exceptions. Refer to Prog 2 in the question. – Pranav Kumar Dec 26 '18 at 07:45
  • 1
    @PranavKumar unchecked exceptions don't have to be handled, so you are always free to throw them (since the caller doesn't have to know about them). In fact, you can throw the NullPointerException even if you remove the throws clause. – Eran Dec 26 '18 at 07:47
  • Because unchecked exceptions don’t need to be declared. From the method signature the method in the superclass could have thrown a `NullPointerException` too, so the subclass doesn’t introduce any new exception. – Ole V.V. Dec 26 '18 at 07:48
-1

Effective signature of method that is getting overridden must be same. Here effective means, change in return type, subclasses of thrown exception in method signature.