简体   繁体   English

如何在 try/catch 块的 IOException 捕获上摆脱 IntelliJ 警告/错误?

[英]How to get rid of IntelliJ warnings/errors on try/catch block's IOException catch?

I'm sorry.抱歉。 I'm sure this has been answered somewhere here before.我确定这之前已经在某处得到了回答。 However, I can't find it.但是,我找不到它。

Essentially, I have a try/catch block where I seem to get a warning or error no matter what I do.本质上,我有一个 try/catch 块,无论我做什么,我似乎都会收到警告或错误。 catch(IOException e) results in a "too broad" warning. catch(IOException e)导致“过于广泛”的警告。 catch (FileNotFoundException e) results in errors from code that requires an IOException catch. catch (FileNotFoundException e)导致需要 IOException catch 的代码出错。 catch (FileNotFoundException | IOException e) results in a "types in multi-catch must be disjoint" error. catch (FileNotFoundException | IOException e)导致“multi-catch 中的类型必须不相交”错误。 Finally, putting two catch blocks (one for each exception) results in a "'catch' branch identical to 'FileNotFoundException'" warning.最后,放置两个 catch 块(每个异常一个)会导致“'catch' 分支与 'FileNotFoundException' 相同”警告。

I don't want to edit IntelliJ's warning system as it is useful.我不想编辑 IntelliJ 的警告系统,因为它很有用。

How can I make the try/catch block below work without warnings or errors?如何使下面的 try/catch 块在没有警告或错误的情况下工作?

@Override
public void readFile(File inFile) {

try {
    FileReader fr = new FileReader(inFile);
    BufferedReader br = new BufferedReader(fr);

    // get the line with the sizes of the puzzle on it
    String sizes = br.readLine();

    // find the height of the puzzle
    int height = Character.getNumericValue(sizes.charAt(0));

    // create a puzzleArr with a height
    this.puzzleArr = new char[height][];

    // create the char[] array of the puzzle itself
    int ct = 0;
    String puzzleLine;

    // add the puzzle to puzzleArr
    while (ct < this.puzzleArr.length) {

        puzzleLine = br.readLine().replaceAll("[^a-z]", "");
        this.puzzleArr[ct++] = puzzleLine.toCharArray();
    }

    // create the LinkedList<char[]> of words to find in the puzzle
    String line = br.readLine();
    while (line != null) {

        line = line.toLowerCase().replaceAll("[^a-z]", "");
        this.wordList.add(line.toCharArray());

        line = br.readLine();
    }

    br.close();

} catch (FileNotFoundException e) {

    e.printStackTrace();
}
}

You can find the corresponding inspection at Settings > Editor > Inspections > Java > Error Handling > Overly broad 'catch' block . 您可以在Settings > Editor > Inspections > Java > Error Handling > Overly broad 'catch' block找到相应的检查。 I would like to note that this inspection was not enabled by default for me. 我想请注意,默认情况下我没有启用此检查。

There are a couple ways to "fix" the warning. 有几种方法可以“修复”警告。

Use Multi-Catch 使用Multi-Catch

@Override
public void readFile(File file) {

    try {
        ...
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

Modify Inspection 修改检查

This is probably the preferred option, especially if you want to avoid disabling the inspection completely. 这可能是首选方案,特别是如果您想避免完全禁用检查。 From the description of the inspection: 从检查的描述:

Reports catch blocks which have parameters which are more generic than the exceptions thrown by the corresponding try block. 报告捕获具有参数的块,这些参数比相应的try块抛出的异常更通用。

Use the first checkbox below to have this inspection only warn on the most generic exceptions. 使用下面的第一个复选框让此检查仅警告最常见的异常。

Use the second checkbox below to ignore any exceptions which hide other exceptions, but which may be thrown and thus are technically not overly broad. 使用下面的第二个复选框忽略任何隐藏其他异常的异常,但这些异常可能会被抛出,因此在技术上不会过于宽泛。

The last paragraph is what we're interested in. 最后一段是我们感兴趣的。

在此输入图像描述

尝试catch (Exception e){}它应该工作

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM