Skip to main content
added 55 characters in body
Source Link

BelowCan you tear my code apart with code review comments? Below is a simple method which reads an XLSX file and does some stuff with it. I use FileInputStream and XSSFWorkbook to do the processing. The code block which I am concerned about is the exception handling (I have been told that it's buggy/not ideal).

Note: I am not allowed to use try-with-resources for some reasons.

/**Process XLSX file.
 * @param filePath Path of file to process
 * @throws Exception
 */
private void processXlsxFile (String filePath) throws IOException {
    String xlsxFileContent = "";
    FileInputStream fis = null;
    XSSFWorkbook workbook = null;
    try {
        File file = new File(filePath);
        fis = new FileInputStream(file);
        workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheetAt(0);
        //Do stuff and build 'xlsxFileContent'.
    } catch (FileNotFoundException fe) {
        logger.error(fe.getMessage());
    } catch (IOException ie) {
        logger.error(ie.getMessage());
    } finally {
        try {
            if (workbook != null) {
                workbook.close();
            }
        } catch (IOException ex) {
            logger.error("Exception while closing XSSFWorkbook: " + ex.getMessage());
        }
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException ex) {
            logger.error("Exception while closing FileInputStream: " + ex.getMessage());
        }
    }
}

Below is a simple method which reads an XLSX file and does some stuff with it. I use FileInputStream and XSSFWorkbook to do the processing. The code block which I am concerned about is the exception handling (I have been told that it's buggy/not ideal).

Note: I am not allowed to use try-with-resources for some reasons.

/**Process XLSX file.
 * @param filePath Path of file to process
 * @throws Exception
 */
private void processXlsxFile (String filePath) throws IOException {
    String xlsxFileContent = "";
    FileInputStream fis = null;
    XSSFWorkbook workbook = null;
    try {
        File file = new File(filePath);
        fis = new FileInputStream(file);
        workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheetAt(0);
        //Do stuff and build 'xlsxFileContent'.
    } catch (FileNotFoundException fe) {
        logger.error(fe.getMessage());
    } catch (IOException ie) {
        logger.error(ie.getMessage());
    } finally {
        try {
            if (workbook != null) {
                workbook.close();
            }
        } catch (IOException ex) {
            logger.error("Exception while closing XSSFWorkbook: " + ex.getMessage());
        }
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException ex) {
            logger.error("Exception while closing FileInputStream: " + ex.getMessage());
        }
    }
}

Can you tear my code apart with code review comments? Below is a simple method which reads an XLSX file and does some stuff with it. I use FileInputStream and XSSFWorkbook to do the processing. The code block which I am concerned about is the exception handling (I have been told that it's buggy/not ideal).

Note: I am not allowed to use try-with-resources for some reasons.

/**Process XLSX file.
 * @param filePath Path of file to process
 * @throws Exception
 */
private void processXlsxFile (String filePath) throws IOException {
    String xlsxFileContent = "";
    FileInputStream fis = null;
    XSSFWorkbook workbook = null;
    try {
        File file = new File(filePath);
        fis = new FileInputStream(file);
        workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheetAt(0);
        //Do stuff and build 'xlsxFileContent'.
    } catch (FileNotFoundException fe) {
        logger.error(fe.getMessage());
    } catch (IOException ie) {
        logger.error(ie.getMessage());
    } finally {
        try {
            if (workbook != null) {
                workbook.close();
            }
        } catch (IOException ex) {
            logger.error("Exception while closing XSSFWorkbook: " + ex.getMessage());
        }
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException ex) {
            logger.error("Exception while closing FileInputStream: " + ex.getMessage());
        }
    }
}
Source Link

Handling exceptions of FileInputStream and XSSFWorkbook

Below is a simple method which reads an XLSX file and does some stuff with it. I use FileInputStream and XSSFWorkbook to do the processing. The code block which I am concerned about is the exception handling (I have been told that it's buggy/not ideal).

Note: I am not allowed to use try-with-resources for some reasons.

/**Process XLSX file.
 * @param filePath Path of file to process
 * @throws Exception
 */
private void processXlsxFile (String filePath) throws IOException {
    String xlsxFileContent = "";
    FileInputStream fis = null;
    XSSFWorkbook workbook = null;
    try {
        File file = new File(filePath);
        fis = new FileInputStream(file);
        workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheetAt(0);
        //Do stuff and build 'xlsxFileContent'.
    } catch (FileNotFoundException fe) {
        logger.error(fe.getMessage());
    } catch (IOException ie) {
        logger.error(ie.getMessage());
    } finally {
        try {
            if (workbook != null) {
                workbook.close();
            }
        } catch (IOException ex) {
            logger.error("Exception while closing XSSFWorkbook: " + ex.getMessage());
        }
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException ex) {
            logger.error("Exception while closing FileInputStream: " + ex.getMessage());
        }
    }
}