public int countChars3(String fileName) {
int total = 0;
FileReader r = null;
try {
r = new FileReader(fileName);
} catch (FileNotFoundException e) {
System.out.println("File named " + fileName + " not found. " + e);
total = -1;
}
try {
while (r.ready()) {
try {
r.read();
} catch (IOException e) {
System.out.println("IOException" + "occurred while counting " + "chars. " + e);
total = -1;
}
total++;
}
} catch (IOException e) {
System.out.println("IOExceptionoccurred while counting chars. " + e);
total = -1;
}
try {
r.close();
} catch (IOException e) {
System.out.println("IOExceptionoccurred while counting chars. " + e);
total = -1;
}
return total;
}
The above code is an example of a tangled mess try-catch block. By reading through the code, they do look messy, there are several nested try-catches. In broad strokes, what is this tangled mess code block trying to demonstrate?
Just have the method throw it and let the client deal with it, also you should prob have a finally{} to close the resource.
Also are you meaning to count chars or bytes, if bytes your entire code can be replaced with: