-2

I have class that throws higher checked exception in overridden method. I know it is not allowed, but why this code works?

package personaltestlevel1;



public class OverrideExcept {

    public static void main(String args[]) {

        S1 s = new S2();
        try
        {
        s.show(); 
        }catch (NullPointerException e){
        System.out.printf(e.getMessage());
        }
    }  

}

class S1{


    public  void show() throws  NullPointerException {
        try 
        {
        System.out.println("not overriden");
        }catch (Exception e){
        throw new NullPointerException();
        }
    }
}

class S2 extends S1{

    public  void show() throws  RuntimeException {
        try
        {
        System.err.println("overriden");
        }catch (Exception e){
        throw new RuntimeException();}


    }
}

I have updated my sample with checked exception - it works anyway.

vico
  • 17,051
  • 45
  • 159
  • 315

2 Answers2

1

Lets take as an example the code below (with some checked-exceptions):

public class Parent {
    public void m() throws Exception {

    }
}

public class Child extends Parent {
    public void m() throws FileNotFoundException {

    }
}

public class Client {
    public void test() {
        Parent p = new Child();
        try {
            p.m();
        } catch (Exception e) {         
            e.printStackTrace();
        }
    }
}

Even though the m in Parent only declares Exception in the throws clause, the Child is allowed to declare FileNotFoundException because FileNotFoundException is-a Exception.

And if you look at the Client which invokes m on Parent can catch the FileNotFoundException thrown by m in Child (the actual object), by just declaring Exception in it's catch.

I think that explains why it doesn't make sense to allow the overriding method to throw a checked-exception which isn't the same or the child of the one in the method being overridden.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • Exactly, you can throw sub-type of any exception from overridden method, as handling the exception itself will handle its sub-type as well. :-) – Ankur Shanbhag Mar 21 '13 at 19:27
0

You cannot throw higher or broader exception in overridden method because if you refer to derived class object using super class reference(polymorphism) then compiler will just check the exception type thrown by super class version of overridden method and not the actual derived class implementation of the method, which is resolved at Runtime.

Here you can throw broader exception because it is a unchecked exception (i.e. compiler does not bother and check the type for unchecked exceptions).

But it is a bad programming practice to throw RuntimeException from a method.

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
  • "But it is a bad programming practice to throw RuntimeException from a method." I think the only possible places are method and constructor. – Bhesh Gurung Mar 21 '13 at 19:18
  • I mean you should handle such exceptions in your code itself or wrap them inside checked exception before throwing, because as they are unchecked exceptions compiler will not stop the calling program to handle it and may result in complete disaster. If you cannot handle it in the method, then wrap it inside checked exception and throw the checked exception, so that calling program will handle it accordingly. – Ankur Shanbhag Mar 21 '13 at 19:22
  • I have updated my sample with checked exception - it works anyway. – vico Mar 21 '13 at 19:24
  • @user1501700: Are you aware of what checked and unchecked exceptions are? – Bhesh Gurung Mar 21 '13 at 19:25
  • Checked exceptions are the one's which needs to be handled by the calling program or else the compiler will throw an error message. Unchecked exceptions are the one's which may occur accidentally or due to some sporadic behavior and hence calling program does not bother bother about handling them. Please refer few useful links : http://www.javapractices.com/topic/TopicAction.do?Id=129, http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation, http://javapapers.com/core-java/java-exception/explain-type-of-exceptions-or-checked-vs-unchecked-exceptions-in-java/ – Ankur Shanbhag Mar 21 '13 at 19:31