java异常处理查询
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() );
}
}
} 问题
似乎很简单,我在“throws IOException”行中收到错误,我实现的 try 和 catch 方法有什么问题吗?
这段代码有两部分,一是读取文件 xanadu.txt,二是复制获取员工数据。两者都有 try 和 catch 实现。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是你完全错误的部分。
并且
throws IOException
放置在错误的位置,应该放置在这里:public static void main(String[] args) throws IOException {
在这种情况下,您不需要任何 try、catch 块 - 您只需将该异常传递给覆盖方法(在您的情况下,您不需要担心它)以让它处理抛出的异常,但如果您想使用 try、catch 块处理异常,则不需要那。
This is the part You got it totally wrong.
and that
throws IOException
is 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.
throws 子句在方法声明中有效,但在方法体内无效。
throws clause is valid on the method declaration and not inside the method body.