-1

Is there a particular way on how to handle errors if my FileInputStream cannot find the inputted file name in its directory? I'm just trying to make it so if my program can't find the file that was typed it'd print an error stating "file doesn't exist"

EDIT: Left out info. Hoping to let users reinput the file name.

WonderfulWonder
  • 515
  • 1
  • 8
  • 20
  • 4
    Catch FileNotFoundException / whatever Exception is appropriate, print error, ???, profit? – PakkuDon May 17 '15 at 14:45
  • This is a matter of opinion, and largely depends on the form of your application. A GUI application would probably best serve its user by popping up a dialog which advises that the specified file path does not exist and asking them to try again. For a console application a simple line of output asking for another attempt at a file path would do the same job. – Bobulous May 17 '15 at 14:50
  • Would it be possible to make the program continue (retell users to reinput the file name)? I was under the impression that catches will end the program. – WonderfulWonder May 17 '15 at 14:51

1 Answers1

1

Should be as simple as this

try {
    <your code>
} catch(FileNotFoundException e) {
    e.printStackTrace();
}

The catch does not end your program, you can easily put a loop around the whole block and try it with a different file name each time.

Nevertheless it would be better to test if the file exists, before you try to open it.

new File("filename").exits()
NaN
  • 7,441
  • 6
  • 32
  • 51