1

I am currently writing a Text Editor using linked lists, and I am pretty much done but I come across a FileNotFoundException when trying to test my program's command line, even though I declared it to be thrown.

Here is the skeleton for my Editor:

public class Editor  {

  public Editor() { 

  }

  public void commandLine() throws FileNotFoundException {

  }
}

Here is the driver for my program:

public class EditorTest
{
  public static void main(String[] args) 
  {
        Editor asdf = new Editor(); 
        asdf.commandLine();

    }
}

I am still getting an error for an unreported FileNotFoundException even though I declared it to be thrown in my command line method. What is wrong?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
dmetal23
  • 141
  • 2
  • 3
  • 13
  • You `throw` it to `main`, but you don't `catch`/`throw` it there. How does this even compile? – Maroun Jul 07 '13 at 04:45

3 Answers3

3

You need to add throws FileNotFoundException to your main method. Or, you can add:

    try {
        asdf.commandLine();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

to your main method, depending on what you need to do based on that exception.

fvrghl
  • 3,642
  • 5
  • 28
  • 36
0

Yo need to declare it on main, too

public static void main(String[] args) throws FileNotFoundException {
Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
0

Declaring an Exception to be thrown in a method (i. e. using throws MyException) doesn't prevent the exception to be thrown, it rather allows the method to throw it for a caller of that method to have to catch that Exception

morgano
  • 17,210
  • 10
  • 45
  • 56