Originally posted by Manish Singhal:
I am getting the below error while compiling the code, please explain:
MyCatch.java:9: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
FileInputStream f1 = new FileInputStream(f);
The line of code
FileInputStream f1 = new FileInputStream(f);
When you read in the API you will see that this throws an exception. If it throws an exception, the line of code must be placed in a try catch block that catches all of the exceptions.
You can also catch the biggest exception and deal with that one.
Example, for your code you could use either of the following three pieces of code...
try{
FileInputStream f1 = new FileInputStream(f);
}catch(FileNotFoundException fnfe){}
catch(IOException ioe){}
or
try{
FileInputStream f1 = new FileInputStream(f);
}catch(IOException ioe){}
or
try{
FileInputStream f1 = new FileInputStream(f);
}catch(Exception e){}
The first one is preferable
Cheers,
Rachel