aspose file tools*
The moose likes Beginning Java and the fly likes help compiling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help compiling" Watch "help compiling" New topic
Author

help compiling

charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49


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

Joined: Oct 27, 2005
Posts: 19544
    
  16

Exceptions


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Enrique Arguelles
Greenhorn

Joined: Feb 09, 2009
Posts: 10
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

Joined: Oct 27, 2005
Posts: 19544
    
  16

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

Joined: Aug 31, 2004
Posts: 11343

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
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
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

Joined: Oct 27, 2005
Posts: 19544
    
  16

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

Joined: Nov 01, 2008
Posts: 49
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

Joined: Mar 24, 2008
Posts: 3775

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,



SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

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

Joined: May 12, 2008
Posts: 229
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help compiling
 
Similar Threads
Incompatible operand types Scanner and int
Can somebody help me understand this problem?
.txt file to char array
Student schedule
Parsing a text file to an arraylist