【问题标题】:When might an IOError be thrown?什么时候可能会抛出 IOError?
【发布时间】:2026-01-29 21:55:01
【问题描述】:

我从未见过抛出IOError 的情况。文档对IOError 的唯一评价是:

发生严重的 I/O 错误时抛出。

没有任何子类或其他明显的东西。

有没有在java中抛出IOError的情况?可能是什么原因造成的?

(不要与IOException 混淆——IOException 在很多情况下都会抛出,并且很常用;我知道。我想知道不太常见的IOError)。

【问题讨论】:

  • @Harvtronix 问题是关于IOError,而不是IOException
  • @Harvtronix 是的,我最初链接到 IOException。但我指的是 IOError。好像人们把这两者混为一谈了?为了清楚起见,我可能需要对此进行编辑(编辑:我已对此进行了编辑)
  • 哎呀!我的错。对不起!
  • 这个问题对我来说已经很清楚了,但是已经发布的所有三个答案(一个已删除)似乎都被误解了。

标签: java ioerror


【解决方案1】:

ConsolePath#toAbsolutePathPath#toUri 声明要抛出此特定异常。当然,这是文档事实,而不是实际声明;因为Error 是运行时异常,所以在签名中声明它是没有意义的。

从它在代码中的样子来看,Console#readLineConsole#readPassword 捕获通过其正常操作产生的 IOException然后将其传播到 IOError

本质上,IOError 表示底层文件系统的严重故障,或访问将 Java 与文件系统联系起来的某些资源。它不会经常被抛出,但如果文件系统内发生严重的事情,它就有可能被抛出。

【讨论】:

  • 您可以查看list of usages in the code。在 JDK 中,我只能找到 Consolesun.nio.fs.WindowsPathsun.nio.fs.WindowsLinkSupport
  • @PhilippWendler:我在 Linux 机器上,所以不会为我抛出这些。我或多或少涵盖了何时提出的一般情况。
  • “因为错误是运行时异常” - 这是不正确的陈述。 RuntimeExceptionException 的子类型。 ErrorThrowable 的另一个类别。
  • @hfontanez - 我确实说这是一个“运行时异常”,因为它可以在运行时出现,而不是它是 RuntimeException 的子类,它是确实非常不同。这两个异常都是在运行时引发的。只有RuntimeExceptions 或其子级才能被应用程序捕获或处理。
  • 另外@hfontanez 只有Exceptions 需要声明被抛出。因此,其他所有内容都不需要声明。也就是说,不需要声明每个实际的运行时异常,因为在应用程序运行期间没有可行的方法来处理IOError
【解决方案2】:

有没有在java中抛出IOError的情况?

import java.io.IOError;

public class Test {

    public static void main(String[] args) {
        throw new IOError(new Exception());
    }

}

会导致

Exception in thread "main" java.io.IOError: java.lang.Exception
    at test.Test.main(Test.java:13)
Caused by: java.lang.Exception
    ... 1 more
Java Result: 1

我相信你期待一个更有可能发生的案例。

IOError 例如在尝试从输入流已关闭的控制台读取时抛出。

你可以尝试运行这个sn-p

import java.io.*;

public class Test {

    public static void main(String[] s) {
        Console con = System.console();
        try {
            InputStreamReader reader = new InputStreamReader(System.in);
            reader.close();
            String st = con.readLine("%s", "Enter a line");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IOError error) {
            error.printStackTrace();
        }
    }
}

这将导致

java.io.IOError: java.io.IOException: Stream Closed
    at java.io.Console.readLine(Console.java:254)
    at Test.main(Test.java:10)
Caused by: java.io.IOException: Stream Closed
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:246)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.io.Console$LineReader.read(Console.java:437)
    at java.io.Console.readline(Console.java:376)
    at java.io.Console.readLine(Console.java:250)
    ... 1 more

【讨论】:

    【解决方案3】:

    这是来自 Oracle 的 Mark Reinhold 的解释:

    新的 IOError 类是结合新定义的 java.io.Console 类。它用于在以下情况下使用 发生不可恢复的 I/O 错误,最合适的响应是 终止程序而不是尝试处理异常。

    IOError 类以及许多其他增强功能将 记录在即将在 JCP 中进行的 Mustang 维护审查中。

    http://cafe.elharo.com/blogroll/undocumented-changes-in-java-6-mustang-ioerror/

    【讨论】:

      【解决方案4】:

      IOError 很少使用。我认为它的主要用例在java.io.Console#readLine()readPassword() 中,默认情况下它们不会抛出 IOExeption(而是包装它)以发出 I/O 问题的信号。

      我猜这样做的动机是,他们很少不想声明检查异常。当终端出现问题时可能会发生这种情况 - 现在您不再有串行线路,只会在严重的系统条件下发生,例如内存或句柄耗尽。

      【讨论】:

        【解决方案5】:

        要查找的一个官方来源是Java Bug Database,您可以在其中使用搜索关键字搜索涉及IOError 的错误。这可以显示一些涉及此错误的真实案例。

        一个直接引用此错误的事件(至少这是我能够找到的)是在处理Console.readLine()JDK-6347312

        JDK 中的用法也很少。很可能它用于发出“关键”IOException 类异常的信号,调用者不应处理该异常(与其他运行时异常一样)。

        【讨论】:

        • 一个奇怪的答案。如果没有任何与 IOError 相关的错误怎么办?这真的证明它从未被使用过吗?
        • @EJP 我的目的是展示发生IOError 的情况的真实示例,而不是关于它的错误。我已经删除了关于它很少使用的部分。
        【解决方案6】:

        IOError 是运行时异常并被归类为错误类别,它是未经检查的异常。对我来说,这似乎发生在您使用 JVM 对底层操作系统系统调用的 JNI/本机调用与系统交互时。这可能是为了访问 IO 设备(存储、键盘、显示器、网络等)。但我几乎没有看到它在 Java API 文档中使用过。最可能的原因是实现者希望将对底层系统的依赖降至最低。

        【讨论】:

          【解决方案7】:

          除此之外,RedHat JBoss Wildfly 域管理库显式地为其ConsoleWrapper 接口抛出IOError。我见过的唯一实现,到目前为止的接口是JavaConsole 类。

          来源:

          /*
           * JBoss, Home of Professional Open Source.
           * Copyright 2012, Red Hat, Inc., and individual contributors
           * as indicated by the @author tags. See the copyright.txt file in the
           * distribution for a full listing of individual contributors.
           *
           * This is free software; you can redistribute it and/or modify it
           * under the terms of the GNU Lesser General Public License as
           * published by the Free Software Foundation; either version 2.1 of
           * the License, or (at your option) any later version.
           *
           * This software is distributed in the hope that it will be useful,
           * but WITHOUT ANY WARRANTY; without even the implied warranty of
           * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
           * Lesser General Public License for more details.
           *
           * You should have received a copy of the GNU Lesser General Public
           * License along with this software; if not, write to the Free
           * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
           * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
           */
          
          package org.jboss.as.domain.management.security.adduser;
          
          import java.io.IOError;
          import java.util.IllegalFormatException;
          
          /**
           * Wrap the console commands
           *
           * @author <a href="mailto:flemming.harms@gmail.com">Flemming Harms</a>
           */
          public interface ConsoleWrapper<T> {
          
              /**
               * Writes a formatted string to this console's output stream using
               * the specified format string and arguments.
               * see <a href="../util/Formatter.html#syntax">Format string syntax</a>
               * @param fmt
               * @param args
               */
              T format(String fmt, Object ...args) throws IllegalFormatException;
          
              /**
               * A convenience method to write a formatted string to this console's
               * output stream using the specified format string and arguments.
               *
               * @param format
               * @param args
               * @throws IllegalStateException
               */
              void printf(String format, Object ... args) throws IllegalFormatException;
          
              /**
               * Provides a formatted prompt, then reads a single line of text from the
               * console.
               *
               * @param fmt
               * @param args
               * @return A string containing the line read from the console, not
               *          including any line-termination characters, or <tt>null</tt>
               *          if an end of stream has been reached.
               * @throws IOError
               */
              String readLine(String fmt, Object ... args) throws IOError;
          
              /**
               * Provides a formatted prompt, then reads a password or passphrase from
               * the console with echoing disabled.
               *
               * @param fmt
               * @param args
               * @return  A character array containing the password or passphrase read
               *          from the console.
               * @throws IOError
               */
              char[] readPassword(String fmt, Object ... args) throws IllegalFormatException, IOError;
          
              /**
               *  Return the console object
               *
               * @return Return the console object
               */
              T getConsole();
          }
          

          【讨论】:

            【解决方案8】:

            我不知道为什么这么多人说错误是运行时异常。他们不是。只需查看 API。 Error 是与 Exception 不同的 Throwable 类别。 RuntimeExceptionException 的子类型。

            简单来说,Error(及其子类型)用于表示一个合理的应用程序不应试图捕捉的严重问题。另一方面,Exception 用于表示一个合理的应用程序可能想要捕捉的条件。它并不是说您必须。它声明 MIGHT 的原因是因为 RuntimeExceptionException 的子类型,并且任何导致运行时异常的条件都应该在开发期间修复(最有可能),以便在部署应用程序时,不应该有任何需要处理这些类型的条件。但是,可以选择继续为它们使用 catch 子句。

            底线:Error 不是RuntimeException。这么说是完全错误的。

            【讨论】: