Java 什么时候可以抛出IOError?

Java 什么时候可以抛出IOError?,java,ioerror,Java,Ioerror,我从来没有看到过一个案例被抛出。文档中关于IOErrorit的唯一说法是: 发生严重I/O错误时引发 没有任何子类或其他明显的东西 java中是否曾经出现过抛出IOError的情况?是什么引起的 (这与--IOException在很多情况下都会抛出,并且很常用;我知道。我想知道不太常见的IOError)。IOError很少使用。我认为它的主要用例是java.io.Console#readLine()和readPassword(),默认情况下,它们不会抛出IOExeption(而是包装它)来表示I

我从来没有看到过一个案例被抛出。文档中关于
IOError
it的唯一说法是:

发生严重I/O错误时引发

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

java中是否曾经出现过抛出
IOError
的情况?是什么引起的


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

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

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

,并声明将引发此特定异常。当然,这是一个文件事实,而不是一个实际的声明;由于
Error
是一个运行时异常,因此在签名中声明将其抛出将毫无意义

根据代码中的外观,
Console#readLine
Console#readPassword
捕获通过正常操作产生的
IOException
,然后将其传播到
IOError


本质上,
IOError
表示底层文件系统的严重故障,或者访问将Java连接到文件系统的某些资源。它不经常抛出,但如果文件系统中发生严重事件,它有可能被抛出。

一个官方来源是使用搜索关键字搜索涉及
IOError
的bug。这可以显示一些涉及此错误的实际案例

直接引用此错误的一个实例(至少我能找到)是处理
Console.readLine()


JDK中也很少有用法。最有可能的情况是,它用于表示调用者不应该处理的“关键”IOException类异常(与其他运行时异常一样)。

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

为了补充这一点,RedHat JBoss Wildfly域管理库为其
控制台包装器
接口显式抛出
IOError
。到目前为止,我看到的接口的唯一实现是
JavaConsole
class

资料来源:

/*
 * 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();
}
/*
*JBoss,专业开源之家。
*版权所有2012,Red Hat,Inc.和个人贡献者
*如@author标记所示。请参阅中的copyright.txt文件
*发布个人贡献者的完整列表。
*
*这是自由软件;您可以重新分发和/或修改它
*根据GNU Lesser通用公共许可证的条款
*由自由软件基金会发布;的2.1版或
*许可证,或(由您选择)任何更高版本。
*
*这个软件的发布是希望它会有用,
*但无任何保证;甚至没有任何关于
*适销性或适合某一特定目的。见GNU
*有关更多详细信息,请参阅较低的通用公共许可证。
*
*您应该已经收到GNU Lesser General Public的副本
*本软件附带的许可证;如果没有,写信给免费的
*软件基金会,51富兰克林ST,第五楼,波士顿,MA
*02110-1301美国,或查看FSF网站:http://www.fsf.org.
*/
包org.jboss.as.domain.management.security.adduser;
导入java.io.IOError;
导入java.util.IllegalFormatException;
/**
*包装控制台命令
*
*@作者
*/
公共接口控制台包装器{
/**
*使用将格式化字符串写入此控制台的输出流
*指定的格式字符串和参数。
*看
*@param-fmt
*@param args
*/
T格式(字符串fmt,对象…args)抛出IllegalFormatException;
/**
*将格式化字符串写入此控制台的
*使用指定格式字符串和参数的输出流。
*
*@param格式
*@param args
*@抛出非法状态异常
*/
void printf(字符串格式,对象…args)抛出IllegalFormatException;
/**
*提供格式化提示,然后从
*控制台。
*
*@param-fmt
*@param args
*@返回包含从控制台读取的行的字符串,而不是
*包括任何行终止字符,或null
*如果已到达流的末端。
*@IOError
*/
字符串读取行(字符串fmt、对象…args)抛出IOError;
/**
*提供格式化提示,然后从中读取密码或密码短语
*已禁用带有回音的控制台。
*
*@param-fmt
*@param args
*@返回包含密码或密码短语读取的字符数组
*从控制台。
*@IOError
*/
char[]readPassword(字符串fmt,对象…args)抛出IllegalFormatException,IOError;
/**
*返回控制台对象
*
*@return返回控制台对象
*/
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
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