What is the criteria for throwing exceptions in subclass

无人久伴 提交于 2019-12-22 05:30:19

问题


What I have known till now is that a subclass if overriding a superclass method should throw the same exception or a subclass of the exception.

For example:

This is correct

class SuperClass {
    public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{
 String signature = "(String, Integer[])";
 System.out.println(str + " " + signature);
 return 1;
 }
}

public final class SubClass extends SuperClass {
    public int doIt(String str, Integer... data) throws ArrayIndexOutOfBoundsException {
        String signature = "(String, Integer[])";
        System.out.println("Overridden: " + str + " " + signature);
        return 0;
    }

    public static void main(String... args) {
        SuperClass sb = new SubClass();
        try {
            sb.doIt("hello", 3);
        } catch (Exception e) {
        }
    }
}

This is Incorrect

class SuperClass {
    public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{
 String signature = "(String, Integer[])";
 System.out.println(str + " " + signature);
 return 1;
 }
}

public final class SubClass extends SuperClass {
    public int doIt(String str, Integer... data) throws Exception {
        String signature = "(String, Integer[])";
        System.out.println("Overridden: " + str + " " + signature);
        return 0;
    }

    public static void main(String... args) {
        SuperClass sb = new SubClass();
        try {
            sb.doIt("hello", 3);
        } catch (Exception e) {
        }
    }
}

But my question is, why this code block is considered correct by compiler?

class SuperClass {
    public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{
 String signature = "(String, Integer[])";
 System.out.println(str + " " + signature);
 return 1;
 }
}

public final class SubClass extends SuperClass {
    public int doIt(String str, Integer... data) throws RuntimeException {
        String signature = "(String, Integer[])";
        System.out.println("Overridden: " + str + " " + signature);
        return 0;
    }

    public static void main(String... args) {
        SuperClass sb = new SubClass();
        try {
            sb.doIt("hello", 3);
        } catch (Exception e) {
        }
    }
}

回答1:


This is because in Java every method can throw a RuntimeException (or an Error) at any time. It does not even need to be declared in the throws part of your method signature. So it is possible to also throw a exception which is a super type of the one declared in your overridden method, as long it is still a sub type of RuntimeException.

See Chapter 11 (Exceptions) of the Java Language Specification for the specification for this behavior, especially 11.1.1. The Kinds of Exceptions which defines checked (needs to be specified in throws clause) and unchecked (does not need to be specified in throwsclause) exceptions.




回答2:


When you override a method, you should define the same checked exceptions. In this case, the SuperClass#doIt method declares that throws an ArrayIndexOutOfBoundsException, so every children that override that method should declare the same checked exceptions or its subclasses.

More info:

  • Method overriding and exceptions.



回答3:


To make an attempt to make it simple, RuntimeException and Error don't need to be declared in throws clauses, but are always implied. If you include the implied exceptions in the declaration, your superclass method declaration could be written;

public int doIt(String str, Integer... data)
    throws ArrayIndexOutOfBoundsException, Error, RuntimeException {

That means, the explicit RuntimeException declaration on the subclass method is a subclass of an existing (implied) exception on the superclass method, so it's allowed.




回答4:


If super class method does not declare any exception, then sub class overridden method cannot declare "checked" exception but it can declare "unchecked" exceptions



来源:https://stackoverflow.com/questions/12663701/what-is-the-criteria-for-throwing-exceptions-in-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!