The task gets us to take a pre-Java 7 try-catch-finally and update it using a try-with-resources.
The code shown below is the task is in its untouched original form hence the errors shown when it is verified.
I noticed on Line 14
public static void main(String[] args) throws IOException {
On line 25 & 26catch (IOException e) {
System.out.println("Something went wrong : " + e);
Question
Why is the throws IOExeception on line 14 required when the purpose of the try-catch block is to catch IOExeception?
I note on the Solution for the task the throws IOExeception is omitted and works fine but if you remove it in the task it reports an uncaught IOException.
Any thoughts?
package en.codegym.task.pro.task15.task1503;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
/*
There are a lot of resources
*/
public class Solution {
public static void main(String[] args) throws IOException {
Scanner scanner = null;
BufferedReader bufferedReader = null;
try {
scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
bufferedReader = Files.newBufferedReader(Path.of(fileName));
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Something went wrong : " + e);
} finally {
if (scanner != null) {
scanner.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
}
}
}