Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't overriding methods throw exceptions broader than the overridden method?

Tags:

java

I was going through SCJP 6 book by Kathe sierra and came across this explanations of throwing exceptions in overridden method. I quite didn't get it. Can any one explain it to me ?

The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.

like image 224
arpanoid Avatar asked May 03 '11 20:05

arpanoid


People also ask

Can a overriding method throw new or broader checked exception?

An overriding method can throw any unchecked exceptions, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method.

Can overridden method not throw exception?

While a superclass method throws an exception while overriding it you need to follow the certain rules. According to the 3rd rule, if the super-class method throws certain exception, you can override it without throwing any exception.

What are the rules we need to follow when overriding a method that throws an exception?

We need to follow some rules when we overriding a method that throws an Exception. When the parent class method doesn't throw any exceptions, the child class method can't throw any checked exception, but it may throw any unchecked exceptions.

Why overridden method should not be more restrictive?

You can not make access modifier more restrictive, because that would violate the basic rule of inheritance that a subclass instance should be replacable in place of a superclass instance. For e.g Suppose that Person class has getName public method which is being used by many classes(including non-sub classes).


1 Answers

It means that if a method declares to throw a given exception, the overriding method in a subclass can only declare to throw that exception or its subclass. For example:

class A {    public void foo() throws IOException {..} }  class B extends A {    @Override    public void foo() throws SocketException {..} // allowed     @Override    public void foo() throws SQLException {..} // NOT allowed } 

SocketException extends IOException, but SQLException does not.

This is because of polymorphism:

A a = new B(); try {     a.foo(); } catch (IOException ex) {     // forced to catch this by the compiler } 

If B had decided to throw SQLException, then the compiler could not force you to catch it, because you are referring to the instance of B by its superclass - A. On the other hand, any subclass of IOException will be handled by clauses (catch or throws) that handle IOException

The rule that you need to be able to refer to objects by their superclass is the Liskov Substitution Principle.

Since unchecked exceptions can be thrown anywhere then they are not subject to this rule. You can add an unchecked exception to the throws clause as a form of documentation if you want, but the compiler doesn't enforce anything about it.

like image 162
Bozho Avatar answered Oct 21 '22 17:10

Bozho