Dakota Spencer
Dakota Spencer

Reputation: 37

Catching two exceptions and handling each one differently in java

    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!

Upvotes: 2

Views: 75

Answers (3)

Andy Turner
Andy Turner

Reputation: 140299

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.

Upvotes: 3

Iffo
Iffo

Reputation: 353

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

Upvotes: 0

Mike Palfrey
Mike Palfrey

Reputation: 424

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.

Upvotes: 0

Related Questions