简体   繁体   English

java.io.Closeable:为什么在 close() 中抛出 RuntimeException 有效?

[英]java.io.Closeable: why throwing RuntimeException in close() works?

I do not know why this codes compiles on Java 7:我不知道为什么这些代码在 Java 7 上编译:

class Lamb implements Closeable{
  @Override
  public void close(){
    throw new RuntimeException();   
  } 
}

Method close() from Closeable throws IOException. Closeable 中的 close() 方法抛出 IOException。 When overriding a method the "throws" declaration must remain the same or use a subclass.覆盖方法时,“throws”声明必须保持不变或使用子类。 Now, the RuntimeException is "runtime" so do not need to be declared (I get to this part) BUT it inherits from Exception and not IOException!现在,RuntimeException 是“运行时”,因此不需要声明(我进入这一部分)但它继承自 Exception 而不是 IOException! You can't extend the scope, right?你不能扩大范围,对吧?

the "throws" declaration must remain the same or use a subclass. “throws”声明必须保持不变或使用子类。

That's not true.这不是真的。 What is true is that the throws clause must be the same or more restrictive .正确的是throws子句必须相同或更严格 More restrictive either means throwing a more restrictive type of Exception (ie a subclass), or not declaring that it can throw the exception.更严格的要么意味着抛出更严格的Exception类型(即子类),要么声明它可以抛出异常。 Not being able to throw an exception is more restrictive than being able to.不能抛出异常比能够抛出异常更具限制性。

I should also point out that even though this method does not implicitly have throws IOException in its throws clause, even if it did this would not conflict with throw new RuntimeException();我还应该指出,即使这个方法在它的throws子句中没有隐含地throws IOException ,即使它这样做了,也不会与throw new RuntimeException();发生冲突throw new RuntimeException(); . . Any method can throw a subclass of RuntimeException (aka unchecked exception), no matter what is written after throws .任何方法都可以抛出RuntimeException的子类(即未经检查的异常),无论throws之后写什么。

RuntimeException是否继承自IOException没有任何区别,因为RuntimeException不参与异常检查,正如您在问题中已经承认的那样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么java.lang.AutoCloseable的close方法会抛出异常,但java.io.Closeable的close方法会抛出IOException? - Why close method of java.lang.AutoCloseable throws Exception, but close method of java.io.Closeable throws IOException? 在Android中获取Java.io.closeable错误 - Getting Java.io.closeable Error in Android 在附加的堆栈跟踪中获取了资源但从未释放过。 有关避免资源泄漏的信息,请参阅 java.io.Closeable,为什么会发生这种情况 - A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks, why this happens com.mysql.jdbc.JDBC4ResultSet无法转换为java.io.Closeable - com.mysql.jdbc.JDBC4ResultSet cannot be cast to java.io.Closeable Android:在附加的堆栈跟踪中获取资源但从未发布。 有关避免资源泄漏的信息,请参阅java.io.Closeable - Android: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks 为什么我收到“资源泄漏:”<unassigned Closeable value> 即使我在 Java 中使用 close() 方法也会出错? - Why I get 'Resource leak: '<unassigned Closeable value>' ' error even when I use close() method in Java? Java:在Closeable.close()中取消注册侦听器 - Java: Unregister listeners in Closeable.close() 为什么close()无法自动在Closeable类上调用? - Why close() cannot be called on Closeable classes automatically? Java中如何保证Closeable接口的close()方法的幂等性? - How is in Java the idempotence of the close() method of the Closeable interface ensured? Java Throwing RuntimeException有什么好处 - Java what is the benefit of Throwing RuntimeException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM