方法覆盖的异常问题
Issue in Exception with method overriding
我有以下程序:
class SuperClassWas {
void myM() throws NullPointerException{
System.out.println("Super class myM called.");
}
}
class SubClassWas extends SuperClassWas{
void myM() throws RuntimeException{
System.out.println("Sub class myM called.");
}
}
public class MethodOveridingWithException {
public static void main(String[] args) {
SuperClassWas obj = new SubClassWas();
obj.myM();
}
}
按照规则
If SuperClass declares an exception, then the SubClass can only declare the child exceptions of the exception declared by the SuperClass, but not any other exception.
并且在我上面的程序中,super class method myM
extends NullPointerException
and subclass overridden method myM
extends RuntimeException
and when我尝试 运行 它工作正常的程序。问题是为什么?理想情况下它不应该工作,因为 RuntimeException
不是 NullPointerException
.
的子 class
如有不妥请指正
此规则仅适用于 checked exceptions。
引用自JLS:
More precisely, suppose that B is a class or interface, and A is a
superclass or superinterface of B, and a method declaration n in B
overrides or hides a method declaration m in A. Then:
If n has a throws clause that mentions any checked exception types,
then m must have a throws clause, or a compile-time error occurs.
For every checked exception type listed in the throws clause of n,
that same exception class or one of its supertypes must occur in the
erasure (§4.6) of the throws clause of m; otherwise, a compile-time
error occurs.
If the unerased throws clause of m does not contain a supertype of
each exception type in the throws clause of n, a compile-time
unchecked warning occurs.
你问题中的异常是未经检查的异常。
我的理解如下
If Subclass overriden method throws checked exception then superclass overridden method should throws supertypes of subclass exception or atleast same exception.
If Subclass overriden method throws unchecked exception then superclass overridden method can throws any type of exception or no exception.
If Subclass overriden method throws no exception then superclass overridden method can throws any type of exception or no exception.
我有以下程序:
class SuperClassWas {
void myM() throws NullPointerException{
System.out.println("Super class myM called.");
}
}
class SubClassWas extends SuperClassWas{
void myM() throws RuntimeException{
System.out.println("Sub class myM called.");
}
}
public class MethodOveridingWithException {
public static void main(String[] args) {
SuperClassWas obj = new SubClassWas();
obj.myM();
}
}
按照规则
If SuperClass declares an exception, then the SubClass can only declare the child exceptions of the exception declared by the SuperClass, but not any other exception.
并且在我上面的程序中,super class method myM
extends NullPointerException
and subclass overridden method myM
extends RuntimeException
and when我尝试 运行 它工作正常的程序。问题是为什么?理想情况下它不应该工作,因为 RuntimeException
不是 NullPointerException
.
如有不妥请指正
此规则仅适用于 checked exceptions。
引用自JLS:
More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B, and a method declaration n in B overrides or hides a method declaration m in A. Then:
If n has a throws clause that mentions any checked exception types, then m must have a throws clause, or a compile-time error occurs.
For every checked exception type listed in the throws clause of n, that same exception class or one of its supertypes must occur in the erasure (§4.6) of the throws clause of m; otherwise, a compile-time error occurs.
If the unerased throws clause of m does not contain a supertype of each exception type in the throws clause of n, a compile-time unchecked warning occurs.
你问题中的异常是未经检查的异常。
我的理解如下
If Subclass overriden method throws checked exception then superclass overridden method should throws supertypes of subclass exception or atleast same exception.
If Subclass overriden method throws unchecked exception then superclass overridden method can throws any type of exception or no exception.
If Subclass overriden method throws no exception then superclass overridden method can throws any type of exception or no exception.