i have make the efforts every little thing ns can uncover top top ns internet, and nopoint seems come fix this. Ns am begginns because that help, teltogether me wcap ns to be doing wrong. Ns am brand new to Java. Thanks!

import java.util.Scanner;class File_Scanner windy static void read() Documents credentials_Documents = brand-new File("credentials.txt"); Scanner file_leader = new Scanner(credentials_file); String<> customers = brand-new String<6>; int index_counter = 0; attempt when (file_reader.hasNextLine()) users = file_reader.nextLine(); file_reader.close(); Capture (exception e) System.out.println(e.getClass()); Thins ins mirroring ns adhering to error

Unreporting exception FileNotFoundException; need to be recorded or asserted to be thrvery own


You are watching: Error: unreported exception filenotfoundexception; must be caught or declared to be thrown

java java.util.scanner
Share
enhance thins Concern
Follow
edited Feb 20 "19 at 23:39

*

Reaz Murshed
21.7k1212 gold badges7272 silver- badges9292 bronze title
asked Feb 20 "19 at 23:32

K. KretzK. Kretz
7211 silver- badge1010 copper title
2
include a comment |

5 Answers 5


active oldest Votes
2
i would certainly prefer come recommend come surround the totality reADVERTISEMENT attribute with a try/Record block favor ns following.

income java.util.Scanner;class File_Scanner public static void read() try Data credentials_Data = new File("credentials.txt"); Scanner file_leader = new Scanner(credentials_file); String<> customers = new String<6>; int index_counter = 0; if (file_reader.hasNextLine()) users = file_reader.nextLine(); file_reader.close(); Capture (exemption e) System.out.println(e.getClass()); the concept that try/Capture is come prevent any error that could occur when to run her program. In your case, Scanner file_leader = brand-new Scanner(credentials_file); have the right to throw an error if the credentials_Data ins not uncovered or deleted. Therefore you must cover thins around a shot block i beg your pardon will provide friend an exemption i beg your pardon can it is in handbrought about present Ideal response messAge in ns Capture block.

hope that helps!


Share
improve this price
Follow
answer Feb 20 "19 at 23:37

Reaz MurshedReaz Murmelted
21.7k1212 gold badges7272 silver- badges9292 copper badges
add a comment |


See more: Why Have People Been Rethinking The Microsoft Model, Rethinking Microsoft

2
i agree via the various other answerns as to ns difficulty (ns Scanner can thheat one exception if it can not uncover the file). I haven"ns seen wcap I"d think about the correct Solution though.

String<> customers = new String<6>; inns index_respond to = 0; Data credentials_File = brand-new File("credentials.txt"); attempt (Scanner file_leader = new Scanner(credentials_file)) while (file_reader.hasNextLine()) users = file_reader.nextLine(); index_counter++; Record (FileNotFoundexception e) // handle exception Catch (NoSuchElementexception e) // take care of exemption Capture (IllegalStateexemption e) // handle exception ns try-with-sources statement will immediately close ns Scanner because that you. This will work through any type of class the implementns the AutoClosemaybe interface.

and also in this case, ins puts the statemenns wislim the scope the the try, so exception will be caught.

your exception taking care of is questionable, for this reason ns didn"ns include it. Yet that"s not really the suggest here. Girlfriend have the right to reADVERTISEMENT more about finest methods come manage Exception in Java or how come point out and also handle Exceptions in Java.

There is one dispute the friend need to lens ns exemption bubble up to ns caller. This price defines exactly how to execute that. Yet in this case, the caller doesn"t yes, really understand just how come manage a FileNotFoundException, Due to the fact that ins does not recognize anypoint around the file. Ns Data is defined in thins method.

friend might throw a different, more explanatory exception. Or you can handle ns exemption below through explaininns wcap a credentials.txt is. Or fail over come a default. Or simply log in the exception, aldespite that"ns questionable. If girlfriend carry out that, you have to describe (in a comment) why the ins sufficient.

i included a line to incremenns index_counter simply Since it appeared come be missing.