Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unhandled exception type FileNotFoundException 2

Status
Not open for further replies.

welldefined

Programmer
Mar 9, 2006
62
0
0
GB
Hi,

I am using eclips3.0 and found
String file = "dddd.txt";
FileReader fr = new FileReader(file);
not working. It said:
Unhandled exception type FileNotFoundException
Can you tell me what I am doing wrong? Does eclips3.0 support that?
 
That means that Eclipse has parsed your code and found that you are not catching an exception that the compiler will expect you to catch. This is a nice feature because it will notify you of code that will break before you try and compile it.

To fix this you can highlight the code and use the refactoring tools eclipse has to add a try{}catch{} block around the code or manually by

String file = "dddd.txt";
try{
FileReader fr = new FileReader(file);
} catch (FileNotFoundException fe) {
System.out.println(fe);
}
 
This simply says you need to catch the exception.

Code:
import java.io.*; //above the class
...
//in the method
try {
   FileReader fr = new FileReader(file);
} catch {FileNotFoundException e) {
   System.out.println(e.getMessage());
}

Read up on exceptions. You can get very important informatino from them.

Hope this helps.

"God is a comedian playing to an audience too afraid to laugh."
-- Francois Marie Arouet (Voltaire)
 
I see.......so, for what kind of code we should add try-catch block?
 
I s'pose from an idealogical prospective, anytime an exception is thrown. Exceptions work well for making sure the code executes properly. You can throw your own exceptions if you need to. Some exceptions MUST be caught, as with your example, others do not NEED to be caught, though would be a good idea to.

You will need to catch some exceptions whenever you use java.io and java.sql. There are probably many other times it is necessary, but I don't know them off hand.

Hope this help.

"God is a comedian playing to an audience too afraid to laugh."
-- Francois Marie Arouet (Voltaire)
 
How is the exception "FileNotFoundException" not obvious ?
It means "the file ... is not found" ....

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top