I have referred to earlier threads regarding multiple Exception handling in Java. Yet, when I try to implement in on my code, it fails to compile.
try {
br = new BufferedReader( new FileReader(file_name));
while((r = br.read()) != -1){
char c = (char) r;
System.out.print(c);
} catch (FileNotFoundException e ){
System.out.println("The file was not found.");
System.exit(0);
} catch (IOException e){
System.out.println("There was an error reading the file.");
System.exit(0);
}
Now I know FileNotFoundException is a special case of IOException and must have multiple catch blocks which is exactly what I am doing, yet, the compiler is not letting me compile it.
You forgot }
to close your while
loop. Please correct it.
It should be like this:
try {
br = new BufferedReader( new FileReader(file_name));
while((r = br.read()) != -1){
char c = (char) r;
System.out.print(c);
}
} catch (FileNotFoundException e ){
System.out.println("The file was not found.");
System.exit(0);
} catch (IOException e){
System.out.println("There was an error reading the file.");
System.exit(0);
}