Home > Software engineering >  How to raise and handle a FileNotFoundException() error
How to raise and handle a FileNotFoundException() error

Time:04-08

The task is to throw a FileNotFoundException() exception and handle it. You also need to check if the file exists on the computer and read data from it.

I wrote 2 methods, one searches for a file, the second one reads, but I am completely confused how to handle this exception. Can you suggest how to refactor this code to handle FileNotFoundException. And point out the errors of the code itself, since it is very terrible (I am just starting to learn Java)

public static boolean findFile(String path, String filename) throws FileNotFoundException {
    File f = new File(path   "\\"   filename);
    if (f.exists()) {
        System.out.println("File found");
        return true;
    }
    else {
        System.out.println("File not found, please check that you entered the correct path and file name");
        throw new FileNotFoundException();
    }
}

public static ArrayList<String> readFromFile(String path, String filename) throws FileNotFoundException {
    if(findFile(path,filename)) {
        ArrayList<String> ip = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(path   "\\"   filename))) {
            String line;
            ip.add(br.readLine());
            while ((line = br.readLine()) != null) {
                ip.add(line);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            System.exit(-1);
        }
        return ip;
    }
    else {
        System.out.println("Failed to read the file, check the correct path and file name");
        return null;
    }
}

CodePudding user response:

The way you've defined the findFile method, it is always going to either return true or throw an exception. So it doesn't really make any sense to test the value returned by findFile(), or for that method to even return a value. Instead, you can assume that if the method does not throw an exception, then the file was found. For the case where it wasn't, you want to catch the exception and deal with it. Here's what that all looks like:

public static void findFile(String path, String filename) throws FileNotFoundException {
    File f = new File(path   "\\"   filename);
    if (f.exists()) {
        System.out.println("File found");
    }
    else {
        System.out.println("File not found, please check that you entered the correct path and file name");
        throw new FileNotFoundException();
    }
}

public static ArrayList<String> readFromFile(String path, String filename) throws FileNotFoundException {
    try {
        findFile(path,filename);

        // Code to read the file...
        ...
    }
    catch (FileNotFoundException ex) {
        System.out.println("Failed to read the file, check the correct path and file name");
        return null;
    }
}

CodePudding user response:

The task is to throw a FileNotFoundException() exception and handle it.

The answer to this question depends a lot on context.

If you are running this from the command line, you will want to add a try catch for the exception and then try again if it happens

Exception e;
do {
  e = null;
  try {
    callAMethod();
  } catch(ex) {
    e=ex;
  }
} while (e != null);

If you are doing this from a GUI/Swing, you can re-rethrow as a RuntimeException and use Log4j or Thread.setUncaughtExceptionHandler(..) to log the error and inform the user. The user can then try again

If you are doing this as a webapp, you will want to change the response code to 404 (FileNotFound) and indicate the file that was not found

Lastly, where you throw the FileNow FoundException, you should include a message

    throw new FileNotFoundException(/*incude a string with the file path*/);
  •  Tags:  
  • java
  • Related