Java I/O 错误异常处理:无法访问 IOException 的 catch 块。这个异常永远不会从 try 语句体中抛出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20929621/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:22:42  来源:igfitidea点击:

I/O Error Exception Handling: Unreachable catch block for IOException. This exception is never thrown from the try statement body

javacommand-lineerror-handlingioarguments

提问by Sully Brooks

I'm currently working on an assignment (Java) for school. In the instructions/rubric, I'm told that I need to use I/O Exception Handling for command line arguments and other things.

我目前正在为学校做作业(Java)。在说明/标题中,我被告知我需要对命令行参数和其他内容使用 I/O 异常处理。

**What The Program Needs to do: **

**程序需要做什么:**

Take two command line arguments when it runs, or else throw an error. The rubric includes this requirement : "I/O error handling is done if no command arguments"

运行时取两个命令行参数,否则抛出错误。准则包括此要求:“如果没有命令参数,则完成 I/O 错误处理”

The problem is, every time I try to use an I/O exception catch, I receive this error:

问题是,每次我尝试使用 I/O 异常捕获时,都会收到此错误:

"Unreachable catch block for IOException. This exception is never thrown from the try statement body"

“无法访问 IOException 的 catch 块。这个异常永远不会从 try 语句体中抛出”

The quick fix suggestions I get from Eclipse:
1. Remove Catch clause 2. Replace Catch clause with close

我从 Eclipse 得到的快速修复建议:
1. 删除 Catch 子句 2. 用 close 替换 Catch 子句

Here is my Code:

这是我的代码:

import java.awt.Robot;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;

public class DecipherText {
    public static void main(String[] args) throws IOException { 
        String firstArg;
        String secondArg;

        if (args.length > 0) {
            try {
                firstArg = args[0];
                secondArg = args[1];
            } catch (IOException e) {
                System.out.println("Usage Error: Not enough Arguments");
                System.out.println(e);
                return;
            }
        }

        String inputFile = args[0];
        String outputFile = args[1];
    }

    public static boolean receiver() { 
        return false;
    }

    public static boolean output() { // Outputs The Final File
        return false;
    }
}

My question(s) are:

我的问题是:

  1. If so, what am I doing wrong?
  2. Is this requirement impossible/asking for the wrong usage of the I/O Error exception handling?
  1. 如果是这样,我做错了什么?
  2. 这个要求是不可能的/要求错误使用 I/O 错误异常处理吗?

Any help is greatly appreciated. Thanks, Sully

任何帮助是极大的赞赏。谢谢,苏利

采纳答案by user2570465

I think the problem is that Java will never throw an IOException in the code

我认为问题在于 Java 永远不会在代码中抛出 IOException

firstArg = args[0];
secondArg = args[1];

therefore, there is no need to catch an IOException. An exception you might catch could be ArrayIndexOutOfBoundsException if the user didn't provide 2 arguments.

因此,不需要捕获 IOException。如果用户未提供 2 个参数,您可能会捕获的异常可能是 ArrayIndexOutOfBoundsException。

IOException is sometimes thrown when you're communicating with outside sources such as through a Socket, or reading from a File and some reading of input goes wrong. But determining what's in the args array does not throw an IOException.

当您与外部源通信(例如通过 Socket)或从文件读取时,有时会抛出 IOException 并且某些输入读取出错。但是确定 args 数组中的内容不会引发 IOException。

I'm not quite sure why your teacher specified I/O Exception. Does he mean input output in general or the Java IOException?

我不太清楚为什么你的老师指定了 I/O Exception。他是指一般的输入输出还是 Java IOException?

回答by doomsdaymachine

the reason its saying that is because there isn't any operation going on inside the try block that can possible throw IOException. in other words, there's no way you can get IOException from what's inside your try block. You can try catching ArrayIndexOutOfBoundsException since that can potentially happen in which case also remove the if(args.length > 0) as its no longer needed since you will be catching that error and handling it. Hope this helps.

之所以这么说是因为在 try 块内没有任何可能抛出 IOException 的操作。换句话说,您无法从 try 块内的内容中获取 IOException。您可以尝试捕获 ArrayIndexOutOfBoundsException ,因为这可能会发生,在这种情况下也删除 if(args.length > 0) 因为不再需要它,因为您将捕获该错误并处理它。希望这可以帮助。