public class Employee {
public static void main(String[] args) {
int j=3;
staples[] stemp = new staples[j];
String file_name = "d:/personal/11636470/NetBeansProjects/Employee/src/employee/Xanadu.txt";
throws IOException
{
Scanner s = null;
try {
s = new Scanner(
new BufferedReader(
new FileReader("file_name")));
while (s.hasNext())
{
System.out.println(s.next());
}
} finally
{
if (s != null)
{
s.close();
}
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for ( j=0;j<3;j++)
{
stemp[j] = new staples();
System.out.print("Enter your name : ");
stemp[j].setName(reader.readLine());
System.out.println("Enter your age : ");
stemp[j].setAge(Integer.parseInt(reader.readLine()));
}
for ( j=0;j<3;j++)
{
System.out.println("Employee number:" + j +" name:"+stemp[j].getName()+" Age:"+stemp[j].getAge() );
}
reader.close(); // VERY IMPORTANT TO CLOSE
System.out.println("Program ended");
}
catch(java.io.IOException ex)
{
System.out.println("Error is " + ex.getMessage() );
}
}
}
}
The problem seems to be simple , i am getting an error in the “throws IOException” line , is there anything wrong with the try and catch method I implemented?
There are two parts to this code , one is to read the file xanadu.txt and the other is to copy get employee data.Both have try and catch implemetations.
This is the part You got it totally wrong.
and that
throws IOExceptionis placed in wrong place, it should be placed here:public static void main(String[] args) throws IOException {In that case you would not need any try, catch blocks – you simply pass that exception to overlaying method(in your case you would not need to worry about it) to let it handle thrown exception, but if you want to handle exceptions with try, catch block you will not need that.