Your options for handling Exceptions are to catch them and deal with them immediately, or throw them into a function and propagate the exception to the caller.
For main it makes sense to catch and handle the exception there.
public static void main(String[] args){ try { readFile(); } catch (FileNotFoundException e) {
However, you can also do something like this:
public static void main(String[] args){ readFile(); } public static void readFile() { try { Scanner input = new Scanner(new File("file.txt"));
I would advise against throwing exceptions in main , but after that it really is a question of whether you have a “backup” if something fails. For more information, this question has fantastic details.
source share