java 继承中的java异常处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3520831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java exception handling in inheritance
提问by user392675
just look at program below..
看看下面的程序..
import java.io.*;
import java.rmi.*;
class class1
{
public void m1() throws RemoteException
{
System.out.println("m1 in class1"); } }
class class2 extends class1
{
public void m1() throws IOException
{
System.out.println("m1 in class2");
} }
class ExceptionTest2
{
public static void main(String args[])
{
class1 obj = new class1();
try{
obj.m1();
} catch(RemoteException e){ System.out.println("ioexception"); }
} }
compile time error.....can not override m1() method
编译时错误.....不能覆盖 m1() 方法
Now if I replace RemoteException in parent class with IOException and vice versa in child class. Then it is compiling.
现在,如果我将父类中的 RemoteException 替换为 IOException,反之亦然。然后就是编译。
Any other checked exception combinations are not working here, evenif I am using checked exception which are at same level.
任何其他检查异常组合在这里都不起作用,即使我使用的是同一级别的检查异常。
Now I am confused why overriding is taking place only in one case, not in other cases??? I will realy appreciate your answer.
现在我很困惑为什么覆盖仅在一种情况下发生,而不是在其他情况下发生???我会很感激你的回答。
回答by Madhusudan Joshi
Exceptionsrule in Inheritancegoes like this:
Exceptions规则Inheritance是这样的:
"When a subclass overridesa method in super class then subclass method definition can only specify all or subset of exceptions classes in the throwsclause of the parentclass method(or overridden method)".
“当子类覆盖超类中的方法时,子类方法定义只能throws在parent类方法(或覆盖方法)的子句中指定所有或子集的异常类”。
RemoteExceptioninherits IOException, so RemoteExceptionis a child class and IOEXceptionis a superclass. It means the subclass(class2) method that overrides parent class(class1) method, which throws IOException, can throwRemoteExceptionbut not vice-versa.
RemoteException继承IOException,所以RemoteException是一个子类,IOEXception是一个超类。这意味着覆盖父类(class1)方法的子类(class2)方法抛出IOException,可以throwRemoteException但反之亦然。
回答by Andre Holzner
RemoteExceptionis more specific than IOExceptionso you can declare that the member m1()of the inheriting class throws this. (RemoteExceptioninherits from IOExceptionand thus RemoteExceptionis a special kind of IOException).
RemoteException比IOException您可以声明m1()继承类的成员抛出 this更具体。(RemoteException继承自IOException,因此RemoteException是一种特殊的IOException)。
It does not work the other way round: by specifying that ANYobject of type class1throws a RemoteExceptionin its member m1(), you can't specify that the same method in class2throws something more generic (because class2type objects are also class1type objects).
它不能反过来工作:通过指定任何类型的对象在其成员中class1抛出 a ,您不能指定相同的方法抛出更通用的东西(因为类型对象也是类型对象)。RemoteExceptionm1()class2class2class1
回答by Thomas L?tzer
A RemoteException is an IOEception, but not the other way around. Since class 2 extends class 1, all thrown exceptions must be compatible with the superclass exceptions. When class1.m1 throws an IOException, class2.m1 can throw a RemoteException since that is a specialization of an IOException.
RemoteException 是一个 IOEception,但反过来不是。由于类 2 扩展了类 1,因此所有抛出的异常都必须与超类异常兼容。当 class1.m1 抛出 IOException 时,class2.m1 可以抛出 RemoteException,因为这是 IOException 的特化。
回答by Pierre Gardin
Because two methods with the same signatures cannot throw different kinds of exceptions.
因为具有相同签名的两个方法不能抛出不同类型的异常。

