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?

有帮助吗?

解决方案

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.

其他提示

Yo need to declare it on main, too

public static void main(String[] args) throws FileNotFoundException {

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top