我对try / catch的抛出感到困惑。我在函数中有两个可能的IOExceptions。一,我想赶上并继续。另一个我想为之前处理的函数抛出异常。
如果无法打开文件,通知用户并继续,我想捕获IOException。如果清除目录时出现IOException,我想抛出异常并在调用代码中处理它。
如果它无法打开文件,是否会抛出clearUploads()在捕获异常时抛出的异常?
主要:
output = parseCSV(fileList);
功能:
private static String parseCSV(List<File> fileList) throws IOException {
String returnString = "";
String[] tokens = null;
String currFileName = "";
for(File file: fileList){
currFileName = file.getName();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
}
//do stuff
}
br.close();
} catch (FileNotFoundException e) {
returnString += "Cannot find " + currfileName + "!\n";
} catch (IOException e) {
returnString += "Cannot open " + currFileName + "!\n";
}
}
clearUploads();
if (returnString.equals("")) {
returnString = "Files uploaded and saved successfully";
}
return returnString;
}
private static void clearUploads() throws IOException {
FileUtils.cleanDirectory(new File(filePath));
}
答案 0 :(得分:2)
try
块会捕获它在其范围内的任何内容,并且与catch
部分中的类型兼容,因为clearUploads位于{{1}之外阻止它特别不会被该块捕获。