简体   繁体   中英

Getting FileNotFoundException even though I declared it to be thrown

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.

哟也需要在主要上声明它

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

声明在方法中抛出异常(即使用throws MyException )并不会阻止抛出异常,而是允许方法抛出该方法的调用者必须捕获该异常

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM