Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help compiling

 
charlie mills
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions
 
Enrique Arguelles
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Yule
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 229
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic