1

I'm looking at some code for a program which takes a file input and reads it, returning specifics about the data inside such as the amount of words, lines, and characters. In all of the methods which perform those tasks, a nested try block is used as shown in the method below, taken from the program:

private static int countSentences(String inputFile){
    int numOfSentences=0;
    String delimiters = ".!?";
    try{
        File input = new File(inputFile);
        try (Scanner reader = new Scanner(new FileInputStream(input))) {
            while (reader.hasNextLine()) {
                String line = reader.nextLine();
                for (int i = 0; i < line.length(); i++) {
                    if (delimiters.indexOf(line.charAt(i)) != -1) { 
                        numOfSentences++;
                    }
                }
            }
        }
    }
    catch (Exception ex) {
    }
    return numOfSentences;
}

Now, the thing which gets to me is the nested try blocks purpose. In the same program, I took away the nested try block and left the single one and its contents which in turn, led to no change in the output. Is this nested try block redundant in this case? Or is it there for some exception which I have not handled yet?

Musilix
  • 330
  • 2
  • 14

0 Answers0