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

前端 未结 15 1357
梦谈多话
梦谈多话 2020-11-22 13:47

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

相关标签:
15条回答
  • 2020-11-22 14:24

    say you have super class A with method M1 throwin E1 and class B deriving from A with method M2 overriding M1. M2 can not throw anything DIFFERENT or LESS SPECIALIZED than E1.

    Because of polymorphism, the client using class A should be able to treat B as if it were A. Inharitance ===> Is-a (B is-a A). What if this code dealing with class A was handling exception E1, as M1 declares it throws this checked exception, but then different type of exception was thrown? If M1 was throwing IOException M2 could well throw FileNotFoundException, as it is-a IOException. Clients of A could handle this without a problem. If the exception thrown was wider, clients of A would not have a chance of knowing about this and therefore would not have a chance to catch it.

    0 讨论(0)
  • 2020-11-22 14:25

    Well java.lang.Exception extends java.lang.Throwable. java.io.FileNotFoundException extends java.lang.Exception. So if a method throws java.io.FileNotFoundException then in the override method you cannot throw anything higher up the hierarchy than FileNotFoundException e.g. you can't throw java.lang.Exception. You could throw a subclass of FileNotFoundException though. However you would be forced to handle the FileNotFoundException in the overriden method. Knock up some code and give it a try!

    The rules are there so you don't lose the original throws declaration by widening the specificity, as the polymorphism means you can invoke the overriden method on the superclass.

    0 讨论(0)
  • 2020-11-22 14:30

    In my opinion, it is a fail in the Java syntax design. Polymorphism shouldn't limit the usage of exception handling. In fact, other computer languages don't do it (C#).

    Moreover, a method is overriden in a more specialiced subclass so that it is more complex and, for this reason, more probable to throwing new exceptions.

    0 讨论(0)
  • 2020-11-22 14:33

    Let us take an interview Question. There is a method that throws NullPointerException in the superclass. Can we override it with a method that throws RuntimeException?

    To answer this question, let us know what is an Unchecked and Checked exception.

    1. Checked exceptions must be explicitly caught or propagated as described in Basic try-catch-finally Exception Handling. Unchecked exceptions do not have this requirement. They don't have to be caught or declared thrown.

    2. Checked exceptions in Java extend the java.lang.Exception class. Unchecked exceptions extend the java.lang.RuntimeException.

    public class NullPointerException extends RuntimeException

    Unchecked exceptions extend the java.lang.RuntimeException. Thst's why NullPointerException is an Uncheked exception.

    Let's take an example: Example 1 :

        public class Parent {
           public void name()  throws NullPointerException {
               System.out.println(" this is parent");
           }
    }
    
    public class Child  extends Parent{
         public  void name() throws RuntimeException{
                 System.out.println(" child ");
         }
    
         public static void main(String[] args) {
            Parent parent  = new Child();
            parent.name();// output => child
        }
    }
    

    The program will compile successfully. Example 2:

        public class Parent {
           public void name()  throws RuntimeException {
               System.out.println(" this is parent");
           }
    }
    
    public class Child  extends Parent{
         public  void name() throws  NullPointerException {
                 System.out.println(" child ");
         }
    
         public static void main(String[] args) {
            Parent parent  = new Child();
            parent.name();// output => child
        }
    }
    

    The program will also compile successfully. Therefore it is evident, that nothing happens in case of Unchecked exceptions. Now, let's take a look what happens in case of Checked exceptions. Example 3: When base class and child class both throws a checked exception

        public class Parent {
           public void name()  throws IOException {
               System.out.println(" this is parent");
           }
    }
    public class Child  extends Parent{
         public  void name() throws IOException{
                 System.out.println(" child ");
         }
    
         public static void main(String[] args) {
            Parent parent  = new Child();
    
            try {
                parent.name();// output=> child
            }catch( Exception e) {
                System.out.println(e);
            }
    
        }
    }
    

    The program will compile successfully. Example 4: When child class method is throwing border checked exception compared to the same method of base class.

    import java.io.IOException;
    
    public class Parent {
           public void name()  throws IOException {
               System.out.println(" this is parent");
           }
    }
    public class Child  extends Parent{
         public  void name() throws Exception{ // broader exception
                 System.out.println(" child ");
         }
    
         public static void main(String[] args) {
            Parent parent  = new Child();
    
            try {
                parent.name();//output=> Compilation failure
            }catch( Exception e) {
                System.out.println(e);
            }
    
        }
    }
    

    The program will fail to compile. So, we have to be careful when we are using Checked exceptions.

    0 讨论(0)
  • 2020-11-22 14:35

    Rule of handling check and unchecked exceptions on overridden methods

    -When parent-class method declares no exception, then child-class overriding- method can declare,

     1. No exception or
     2. Any number of unchecked exception
     3. but strictly no checked exception
    

    -When parent-class method declares unchecked exception, then child-class overriding-method can declare,

     1. No exception or
     2. Any number of unchecked exception 
     3. but strictly no checked exception
    

    -When parent-class method declares checked exception, then child-class overriding-method can declare,

     1. No exception or
     2. Same checked exception or
     3. Sub-type of checked exception or
     4. any number of unchecked exception
    

    All above conclusion hold true, even if combination of both checked & unchecked exception is declared in parent-class’ method

    Ref

    0 讨论(0)
  • 2020-11-22 14:37

    The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method.

    Example:

    class Super {
        public void throwCheckedExceptionMethod() throws IOException {
            FileReader r = new FileReader(new File("aFile.txt"));
            r.close();
        }
    }
    
    class Sub extends Super {    
        @Override
        public void throwCheckedExceptionMethod() throws FileNotFoundException {
            // FileNotFoundException extends IOException
            FileReader r = new FileReader(new File("afile.txt"));
            try {
                // close() method throws IOException (that is unhandled)
                r.close();
            } catch (IOException e) {
            }
        }
    }
    
    class Sub2 extends Sub {
        @Override
        public void throwCheckedExceptionMethod() {
            // Overriding method can throw no exception
        }
    }
    
    0 讨论(0)
提交回复
热议问题