HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMinor

Reading a text file, need help cleaning exception handling

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
handlingreadingfileexceptionneedtexthelpcleaning

Problem

Which of the following is cleaner / more adopted standard of handling exceptions while coding a stream ? I would also appreciate a reason why one of the following trumps over other ? In my opinion I find option 1 better but nested try where first try does nothing more than a wrapper is making me not satisfied with approach.

NavigableSet dictionary = new TreeSet();
BufferedReader br = null;
try {
    try {
        br = new BufferedReader(new FileReader("/Users/ameya.patil/Desktop/text.txt"));
        String line;
        while ((line = br.readLine()) != null) {
            dictionary.add(line.split(":")[0]);
        }

    } finally {
        br.close();
    }
} catch (Exception e) {
    throw new RuntimeException("Error while reading dictionary");
}


VS

NavigableSet dictionary = new TreeSet();
BufferedReader br = null;

    try {
        br = new BufferedReader(new FileReader("/Users/ameya.patil/Desktop/text.txt"));
        String line;
        while ((line = br.readLine()) != null) {
            dictionary.add(line.split(":")[0]);
        }

    }
} catch (Exception e) {
    throw new RuntimeException("Error while reading dictionary");
} finally {
     try{
        if(br != null) br.close();
        } catch (Exception ex){

        }
 }

Solution

First off, in Java 7 all of this is obsolete if you use 'try with resources'

NavigableSet dictionary = new TreeSet<>();
try (BufferedReader br = new BufferedReader(new FileReader("/Users/ameya.patil/Desktop/text.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        dictionary.add(line.split(":")[0]);
    }
} catch (Exception e) {
    throw new RuntimeException("Error while reading dictionary");
}


If you have to work on an earlier JDK consider these points :

Both snippets you supply are not exactly equivalent. i.e. if closing the file fails, the first snippet will throw a RuntimeException wrapping the exception of the close(). The second snippet will throw a RuntimeException wrapping the original exception.

Again in SE 7 using the try with resources you'll be able to see both exceptions : the original exception is thrown and it has the exception upon close() attached as a suppressed exception.

So I think style is irrelevant, you want the original exception (which may even not be related to I/O). If you can use SE 7, use that, otherwise go for snippet 2 (add logging for the exception on close())

Code Snippets

NavigableSet<String> dictionary = new TreeSet<>();
try (BufferedReader br = new BufferedReader(new FileReader("/Users/ameya.patil/Desktop/text.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        dictionary.add(line.split(":")[0]);
    }
} catch (Exception e) {
    throw new RuntimeException("Error while reading dictionary");
}

Context

StackExchange Code Review Q#33428, answer score: 5

Revisions (0)

No revisions yet.