This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
If you check the API documentation for that Scanner constructor (the one that takes a single File argument), you will see that it might throw a FileNotFoundException if the source file is not found. The compiler is telling you that you need to take some precautions in case this happens -- in particular, you need to either "catch" or "declare" the exception. See Rob's link on Exceptions for details.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
If you decide to catch the exception, which I'd say in personal experience is more common, I would avoid creating an empty catch block. Do something with the error like simply printing it to a log. Otherwise, later down the line you might run into some trouble when suddenly things don't work but everything looks a-ok.
Paul Yule wrote:I would avoid creating an empty catch block. Do something with the error like simply printing it to a log. Otherwise, later down the line you might run into some trouble when suddenly things don't work but everything looks a-ok.
Excellent advice. The least you should do is print the exception.
Only if you are really really really really really really 100.00000% sure the exception won't occur you can ignore it, but even then you should put a comment on why it won't occur. An example I once wrote, that calls an object's clone method through reflection:
The first two exceptions are guaranteed to not happen, because at the moment invoke is called there is 100% surely a public method called clone() without any parameters. Granted, ignoring the last exception may not be nice, but the Javadoc said that null would be returned upon error.
Joined: Nov 01, 2008
ok i'm confused. the file has already been created and there's data in it. If i use catch like Enrique suggested, will this mean that it will read the data in the file?
charlie mills wrote:ok i'm confused. the file has already been created and there's data in it. If i use catch like Enrique suggested, will this mean that it will read the data in the file?
Just because a method declares that it can throw an exception doesn't mean it always throws it - it just means it can, under certain conditions.
If the file exists then the exception will not be thrown; if however the file is deleted prior to creating the scanner it will.
Joined: May 12, 2008
When a method declares that it can throw an exception it is basically just letting others know of common problems that can occur from using the method. Many times common problems have many different solutions. By declaring the exception the creator of the method is telling you that you should handle this type of situation ahead of time instead of the creator just erroring or handling the problem for you.
For instance, if the file did not exist there you could catch the exception and try another location, or simpy display to the user that the file could not be found and if there was user input you could request them to re-type the file name carefully, or pull up an open file dialog inside your catch logic.