Not Permitted to Use File Not Found Exception, but it is necessary

Tags: , ,



so I am having a strange problem where I am trying to use a try-catch, but the exception it not permitting. I will try to be as clear as I can describing the issue.

Some background: Okay, so the assignment is to basically allow the user to write some files and also read them. And so I’m at the part in my code where I am calling a previously made file. The user gets to type in the file that they wish to call, and so of course this exception is mandatory. But the error says that the exception is never thrown, but when I run the code it does get thrown. Let me show you some code.

`while(error){
                    try{
                        response = scan.nextLine();


                        error = false;
                    }catch(FileNotFoundException e){
                        System.out.println("nERROR:   ");
                        error = true;
                    }
                }`

Okay so this is my written code. Now here is my output (running without a try-catch)

This is my output without using a try-catch

And this is my error:

Exception in thread “main” java.lang.Error: Unresolved compilation problem: Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body

at fileLab2.MemoMaker.main(MemoMaker.java:126)

Hopefully somebody can help with this problem. I made this account just for this question, honestly, it’s pretty annoying. Please ask if you need any clarification.

Here is my entire program:

/*
 * tslaurenx
 * File reader lab 2
 * OOP
 * 11/20/15
 * Lab 8b
 * 
 */

package fileLab2;
import java.util.*
import java.io.*;

public class MemoMaker {

    public static void main(String[] args) throws IOException {


        int choice = 0;
        String name = "", response = "", memo = "";
        boolean running = true, error = true;

        String record = "";


        System.out.print("Hello, and welcome to the Memo Maker 2015. ");

        File memoTitles = new File("Memo Titles.txt");


        PrintWriter writeTitles = new PrintWriter(memoTitles);


        Scanner scan = new Scanner(System.in);


        while(running){

            while(error){
            System.out.println("nPlease select an option.");
            System.out.println("n(1) Create a Memon(2) View an Old Memon(3) Directions for Overwritingn(4) Exit");



                try{
                    choice = scan.nextInt(); 

                    if(choice >4 || choice <1){
                        System.out.println("nERROR: Do you see that number there? Okay then... try again.n");
                        error = true;
                    }//close if
                    else error = false;

                }catch(InputMismatchException e){
                    scan.nextLine();
                    System.out.println("nERROR: You need to enter the NUMBER next to your option.n");
                    error = true;
                } //close catch

            }//close try while

            error = true;

            switch(choice){

            case 1:
                //create a file


                Scanner key = new Scanner(System.in);

                System.out.println("Enter the name of your memo.");
                name = key.nextLine();

                File file = new File(name + ".txt");


                record = record + "n" +file.getName();


                writeTitles.print(record);

                writeTitles.close();


                PrintWriter write = new PrintWriter(file);

                System.out.println("nOkay, now write your memo: ");

                memo = key.nextLine();

                write.println(memo);

                write.close();

                System.out.println("nYour memo has been created.");

                break;


            case 2:
                //view an old file
                Scanner readTitles = new Scanner(memoTitles);
                Scanner enter = new Scanner(System.in);


                if(readTitles.hasNext() == false){
                    System.out.println("There are no files available. Begin with making a memo.");
                    running = true;
                    break;
                }//close if
                else{
                    //view a file

                    System.out.println("Here are your available memos. nEXACTLY type the name of the memo that you wish to view.nn");

                    System.out.println(record);

                    while(error){
                        try{
                            response = enter.nextLine();

                            error = false;
                        }catch(FileNotFoundException e){
                            System.out.println("nERROR:   ");
                            error = true;
                        }
                    }

                    error = true;

                    File trying = new File(response);

                    Scanner readMemo = new Scanner(trying);


                    System.out.println("nHere is the message: ");

                    while(readMemo.hasNext() ){
                        String words = readMemo.nextLine();
                        System.out.println(words);
                    }//close while


                    while(error){
                        System.out.println("nnType 1 when you are finished viewing.");

                        int n = 0;

                        n = scan.nextInt();

                        if(n==1){
                            error = false;
                        }
                        else error = true;
                    }


                    readMemo.close();


                }//close else
                break;
            case 3:

                System.out.println("nGo to Create a Memo, and type in the name of the file you wish to overwrite.n");
                break;
            case 4:
                Scanner ans = new Scanner(System.in);

                int a = 0;

                System.out.println("Are you sure you wish to exit? All of your memos will be lost.");

                System.out.println("1. Yesn2. No");

                while (error){

                    try{
                        a = ans.nextInt();

                        if(a != 1 && a !=2){
                            System.out.println("nERROR: Please enter a valid option.");
                            error = true;
                        }//close if
                        else error = false;
                    }catch(InputMismatchException e){
                        System.out.println("nERROR: Please enter a valid option.");
                        error = true;
                    }//close catch

                } //close try while

                error = true;

                if (a == 1){

                    System.out.println("Until next time!");
                    running = false;

                }// close if
                else {
                    running = true;
                }

            }//close switch




        }//close running

        scan.close();


        writeTitles.close();


    }//close main

}//close class

Answer

This line,

File trying = new File(response);

even if, like you can see below written in the JavaDoc, will not throw any exception should preferrably (but that is not mandatory) be contained within your try...catch and using a FileInputStream (which should be contained inside the try block) because it is the new FileInputStream() constructor which is throwing an exception. Remove the use of Scanner also when trying to read a file efficiently.

Here is a useful link :


Javadoc

public File(String pathname) Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

That means that it does not check if the file exists or not.


Solution

FileInputStream fis = null;
try{
    fis = new FileInputStream(new File(enter.nextLine()));
    error = false;
} catch (FileNotFoundException e){
    // Blabla
}


Source: stackoverflow