-2

So I am making a program that renders pictures from .ppm files. I have got another version working but have now moved on to the other part which is reading multiple images from the same document and to basically use this to animate it with a small delay inbetween switching pictures, and then the following error has come up and am completely stumped by it:

java.io.ioexception is never thrown in body of corresponding try statement

Any help would be much appreciated.

  public void renderAnimatedImage(){       
    String image = UI.askString("Filename: ");
    int keepingCount =0;      //Variables
    int numCount = 1;    
    try{
        Scanner fileScan = new Scanner(image);    // making scanners
        Scanner scan = new Scanner(image);
        File myFile = new File(image);      //making files
        File myFile2 = new File(image);
        int num = 0;

        while(scan.hasNextLine()){
           String Line = scan.nextLine();

           Scanner keywordSc = new Scanner (Line);
           while(keywordSc.hasNext()) {
               String Keyword = keywordSc.next();
               if (Keyword.equals("P3")) {
                   num++;
                }
                else { break; }
            }
        }



        while (keepingCount< numCount) {
            this.renderImageHelper(scan);   // calling upon an earlier method which works.
            keepingCount++;
        }
    }
    catch(IOException e) {UI.printf("File failure %s \n", e); }



}
MBT
  • 21,733
  • 19
  • 84
  • 102
DrSaac
  • 1
  • 1

2 Answers2

4

It means the code you're writing inside your try/catch is never throwing an IOException, which makes the clause unnecessary. You can just remove it and keep your code without it.

Mel A.
  • 41
  • 4
  • 1
    Yes ... but his real problem is (I think) deeper than this. If you just remove the unnecessary `catch`, the real problem is harder to spot. In fact it *should be there* ... – Stephen C May 02 '18 at 14:50
  • Thank you, @stephenC. I saw your answer below and it is much more accurate than mine. – Mel A. May 03 '18 at 13:17
3

I bet that you think there could be an IOException because of this line:

 Scanner fileScan = new Scanner(image);    // making scanners

But that line is not doing what you think it does. Since image is a String this will use the Scanner(String) constructor. But that constructor treats its argument as a string to be scanned, not the name of a file to be scanned.

Hence new Scanner(image) is not doing any I/O and is not declared as throwing an IOException.

And the rest of the code in the block won't throw IOException either. The Scanner next / hasNext methods that you are using will throw a different exception if there is an I/O error while reading. (Check the javadocs.)


Also, you seem to be misunderstanding what File is / does.

File myFile = new File(image);      //making files

The comment is incorrect. That does not make a file.

Actually, it makes a File object which is an in-memory representation of a filename / pathname. Creating a File object doesn't cause a file to be created in the file system. (Again, check the javadocs.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216