Carl5444
Carl5444

Reputation: 25

Java Unreported Exception while using JFileChooser

public FileReader() throws FileNotFoundException {
    //create fileChooser
    JFileChooser chooser = new JFileChooser();
    //open fileChooser
    int returnValue = chooser.showOpenDialog(new JFrame());
    try {
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
        }
    } catch (FileNotFoundException e) {

    }
}

I'm writing a program to read a text file and puts it into an array of Strings (words). This is just the file reader for the program, and it's giving me an error when I try to call the method from another part of the code. Any ideas would be greatly appreciated; its probably a simple fix and I just can't seem to find it.

Here's the error:

unreported exception FileNotFoundException; must be caught or declared to be thrown

Upvotes: 1

Views: 1084

Answers (2)

Makoto
Makoto

Reputation: 106400

You're getting bit by checked exceptions rather hard here. The main thing is that FileNotFoundException is checked, and it must either:

  • be caught in a try...catch statement at its point of use, or
  • declared to be thrown via the throws declaration in the method's signature.

You do not generally want to do both of those at the same time, which you are right now.

As further advice, you also don't want to do either of these:

  • throw an exception in the constructor
  • not do anything with a caught exception

So my personal advice would be to catch the exception and deal with it...

public FileReader()  {
    //create fileChooser
    JFileChooser chooser = new JFileChooser();
    //open fileChooser
    int returnValue = chooser.showOpenDialog(new JFrame());
    try {
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

...but unless I'm looking at the wrong libraries, I don't see anywhere where a FileNotFoundException is even thrown on JFileChooser! That'd make your code simple enough - don't bother* with the try...catch at that point.

*: You'd actually have to remove it since it's a compilation error to catch a checked exception that's never thrown.

Upvotes: 1

Andy Turner
Andy Turner

Reputation: 140319

You have declared the constructor as throwing a checked exception:

public FileReader() throws FileNotFoundException

Anywhere you call this constructor, you must either declare it thrown from that method, e.g.

public static void main(String[] args) throws FileNotFoundException {
  new FileReader();
}

or catch it and handle it, e.g.

public static void main(String[] args) {
  try {
    new FileReader();
  } catch (FileNotFoundException e) {
    // Handle e.
  }
}

or, if nothing in FileReader() throws an uncaught FileNotFoundException (like in your code, where the FileNotFoundException is caught and swallowed), remove the throws FileNotFoundException from FileReader(), allowing e.g.

public static void main(String[] args) {
  new FileReader();
}

Upvotes: 1

Related Questions