• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help compiling

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the fifth line gives me an error message saying that it must be caught or declared to be thrown and i dont understand what this means
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exceptions
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList cities = new ArrayList();

ArrayList temperatures = new ArrayList();
try{
Scanner citiesfile = new Scanner(new File("Cities.txt"));
catch(IOException e){}
while (citiesfile.hasNext())
{
cities.add(citiesfile.nextLine());
}

This is in case the scanner fails to creatre the file.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A scanner doesn't create a file - it reads from an existing file. Besides that, although you have the good idea, your code is... well, let's just say it doesn't come close to compiling.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
charlie mills
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

charlie mills wrote:If i use catch like Enrique suggested, will this mean that it will read the data in the file?



Not exactly the same way (that would give an compiler error because citiesfile is declared inside the block). try this,


 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Paul Yule
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic