When to throw FileNotFoundException

I called a method called readFile() in the main method, readFile() throws FileNotFoundException, but main does not work, as shown below:

 public static void main(String[] args){ readFile(); } public static void readFile() throws FileNotFoundException{ Scanner input = new Scanner(new File("file.txt")); ... } 

When I compiled the program, I got an error in readFile() in the main method. It seems that I need to add an exception to the main header as well. Why do I need to throw an exception in both the main and readFile() headers?

+5
source share
5 answers

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) { // Do something with `e` } } public static void readFile() throws FileNotFoundException { Scanner input = new Scanner(new File("file.txt")); // ... } 

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")); // ... } catch (FileNotFoundException e) { // Do something with `e` or handle it accordingly. } } 

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.

+3
source

Java has some controversy regarding its exceptions. It has two classes of exceptions. Checked and unchecked. Any exception thrown from a RuntimeException or Error is not checked and should not be caught or explicitly declared as capable of a method signing method.

FileNotFound is a checked exception and must be either caught or declared throwable in the method signature.

The basic idea is that checked exceptions are those that you can recover, and excluded exceptions are those that come from the most likely fatal programming error.

Here you can read everything: https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html

+7
source

Think of the throws keyword as a promise; You say that now you will not catch the exception, but you will catch it in the calling statement.

The code that you are currently using in your readFile method seems completely correct, but you need to wrap the call try-catch in a try-catch in order to properly handle the exception.

+5
source

it makes no sense to throw an exception in the main method - this is the entry point to the program, which means that no other method will catch this exception and will not handle it. you have to catch and handle the exception here - either register an error, give a clear message to the user, read some other file, regardless, but the throwing exception is wrong here.

+1
source

You need to catch the exception

 public static void main(String[] args){ readFile(); } public static void readFile() { try { Scanner input = new Scanner(new File("file.txt")); ... } catch (FileNotFoundException ex) { // Error message } catch (Exception ex) { // Incase a different exception is caught } } 
+1
source

Source: https://habr.com/ru/post/1239320/


All Articles