2
    public static void main(String[] args) throws IOException {
    String inputFile = "PullFrom.txt";
    String outputFile = "Output.txt";
    Scanner input = new Scanner(new File(inputFile));
    BufferedWriter out = new BufferedWriter(new FileWriter(outputFile, true));
    int ID;

    while (input.hasNextLine() && input.hasNextInt()) {
        ID = input.nextInt();

         for (int i = 0; i < 1; i++) {
         try {
            out.write("case "+ID+ ":");
            out.newLine();
            out.write("setRandomWalk(false);");
            out.newLine();
            out.write("break;");
            out.newLine();
        } catch (FileNotFoundException e) {
            System.out.println("Can't Find " +inputFile );
            e.printStackTrace();
         } catch (IOException e) {
            System.out.println("Can't create " + outputFile );
         }
}
     out.close();
     input.close();}}}

I am trying to catch FileNotFoundException but my BufferedWriter requires that I have IOException, so I am trying to catch them both.

I am getting this in the console:

Exception in thread "main" java.io.FileNotFoundException: PullFrom.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at reader.PullFrom.main(PullFrom.java:15)

If someone could please explain to me what exactly IOException is as well that would be great, thank you!

3
  • FileNotFoundException is a subclass of IOException: it just indicates a more specific problem that has occurred whilst doing IO-related things. As such, catch (IOException e) would catch both. Commented Nov 18, 2015 at 14:06
  • So if I wanted to handle "both" differently, could I do that and still catch them both with IOException? Commented Nov 18, 2015 at 14:10
  • 1
    If you want to handle them differently, put the catch for the subclass before the catch for the superclass, as you have done. However, you aren't reaching the catch blocks in your code - see my answer. Also, you can't handle both, you can only handle 1. Commented Nov 18, 2015 at 14:12

3 Answers 3

3

You should post a minimal example of the code which generates the problem. This is all that is necessary to generate the exception posted:

public static void main(String[] args) throws IOException {
  String inputFile = "PullFrom.txt";
  String outputFile = "Output.txt";
  Scanner input = new Scanner(new File(inputFile));
}

You are getting a FileNotFoundException here because PullFrom.txt does not exist. Because FileNotFoundException is a subclass of IOException, it is handled by the throws IOException in the method signature.

Because you're not handling the exception in this method, it is being thrown from the main method, and so is being handled by the uncaught exception handler. You'd need to surround the new Scanner(new File(inputFile)) in a try/catch block if you want execution to continue despite the exception.

Sign up to request clarification or add additional context in comments.

8 Comments

I purposefully removed my PullFrom.txt file so that I could set up a sort of error handler to print out "Can't find PullFrom.txt" whenever the file is missing, however it throws an exception to the console window rather than printing out my text.
That's because you're not handling the exception - it is being thrown from the main method, and so is being handled by the uncaught exception handler. You'd need to surround the new Scanner(new File(inputFile)) in a try/catch block.
If I do that, my "input" (which is actually my scanner) variable cannot be resolved.
Declare the input variable before the try/catch block. However, if the exception occurs, input won't be assigned a value - how would you then be able to use it?
How can I declare my input variable when "input" is my scanner. Scanner input = new Scanner(new File(inputFile));
|
0

An IOException would cover other Exceptions rather than just FileNotFoundExceptions - e.g. if the file can't be accessed.

FileNotFoundException is actually a sub class of IOException, so you could just catch IOException and this would work for both cases.

Also - do you mean to do the e.printStackTrace()? This is typically speaking an anti pattern - it looks ugly and it would result in the output you see in your log here.

2 Comments

I did not mean to do the printStackTrace(); Eclipse added it when I added my throw/catch clause. I removed it from the code as well as my FileNotFoundException catch clause but I still get the same output in my log.
The stack-trace is coming from the Java platform itself (the exception gets propagated outside main()), not from the catch blocks.
0

Scanner can throw a FileNotFoundException if source is not found, you should put this:

 Scanner input = new Scanner(new File(inputFile));
 BufferedWriter out = new BufferedWriter(new FileWriter(outputFile, true));

in a block try/catch

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.