如何摆脱 IntelliJ warnings/errors on try/catch 块的 IOException 捕获?
How to get rid of IntelliJ warnings/errors on try/catch block's IOException catch?
对不起。我敢肯定这已经在这里的某个地方得到了回答。但是,我找不到它。
基本上,我有一个 try/catch 块,无论我做什么,我似乎都会收到警告或错误。 catch(IOException e)
导致 "too broad" 警告。 catch (FileNotFoundException e)
导致需要 IOException 捕获的代码出错。 catch (FileNotFoundException | IOException e)
导致 "types in multi-catch must be disjoint" 错误。最后,放置两个 catch 块(每个异常一个)会导致“'catch' 分支与 'FileNotFoundException' 相同”警告。
我不想编辑 IntelliJ 的警告系统,因为它很有用。
如何使下面的 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();
}
}
您可以在Settings > Editor > Inspections > Java > Error Handling > Overly broad 'catch' block
找到相应的检查。我要注意,默认情况下没有为我启用此检查。
有几种方法可以"fix"警告。
使用多重捕捉
@Override
public void readFile(File file) {
try {
...
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
修改检查
这可能是首选选项,特别是如果您想避免完全禁用检查。来自检查的描述:
Reports catch blocks which have parameters which are more generic than the exceptions thrown by the corresponding try block.
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.
最后一段是我们感兴趣的。
对不起。我敢肯定这已经在这里的某个地方得到了回答。但是,我找不到它。
基本上,我有一个 try/catch 块,无论我做什么,我似乎都会收到警告或错误。 catch(IOException e)
导致 "too broad" 警告。 catch (FileNotFoundException e)
导致需要 IOException 捕获的代码出错。 catch (FileNotFoundException | IOException e)
导致 "types in multi-catch must be disjoint" 错误。最后,放置两个 catch 块(每个异常一个)会导致“'catch' 分支与 'FileNotFoundException' 相同”警告。
我不想编辑 IntelliJ 的警告系统,因为它很有用。
如何使下面的 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();
}
}
您可以在Settings > Editor > Inspections > Java > Error Handling > Overly broad 'catch' block
找到相应的检查。我要注意,默认情况下没有为我启用此检查。
有几种方法可以"fix"警告。
使用多重捕捉
@Override
public void readFile(File file) {
try {
...
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
修改检查
这可能是首选选项,特别是如果您想避免完全禁用检查。来自检查的描述:
Reports catch blocks which have parameters which are more generic than the exceptions thrown by the corresponding try block.
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.
最后一段是我们感兴趣的。