Reputation: 141
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?
Upvotes: 1
Views: 79
Reputation: 17422
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
Upvotes: 0
Reputation: 18793
Yo need to declare it on main, too
public static void main(String[] args) throws FileNotFoundException {
Upvotes: 0
Reputation: 3728
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.
Upvotes: 3