在子类中引发异常的标准是什么 [英] What is the criteria for throwing exceptions in subclass

查看:89
本文介绍了在子类中引发异常的标准是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我所知道的是,如果子类重写了超类方法,则应该抛出相同的异常或该异常的子类.

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.

例如:

这是正确的

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) {
        }
    }
}

这是不正确的

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) {
        }
    }
}

但是我的问题是,为什么编译器认为此代码块正确?

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) {
        }
    }
}

推荐答案

这是因为在Java中,每个方法都可以随时抛出RuntimeException(或Error).它甚至不需要在方法签名的throws部分中声明.因此,有可能会抛出一个异常,该异常是您重写的方法中声明的那个的超类型,只要它仍然是RuntimeException的子类型.

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.

请参见Java语言的第11章(异常)规范(针对此行为的规范),尤其是 11.1.1.定义 checked (需要在throws子句中指定)和 unchecked (无需在throws子句中指定)的异常种类 )例外.

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.

这篇关于在子类中引发异常的标准是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆