I'm confused about try/catch with a throws. I have two possible IOExceptions within a function. One, I want to catch and continue. The other I want to throw an exception for the previous function to deal with.
I'd like to catch an IOException if it cannot open a file, notify the user, and continue. If an IOException appears when the directory is cleared, I want to throw an exception and handle it in the calling code.
Will it throw the exception that clearUploads() may throw while catching the exception if it can't open the file?
In main:
output = parseCSV(fileList); Functions:
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)); }
br.close()isn't safe. You should call it infinallyor, better use try-with-resources.