java异常处理查询

发布于 2025-01-02 15:51:54 字数 1527 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初心 2025-01-09 15:51:54

这就是你完全错误的部分。

try
{
   s = new Scanner((Readable) new BufferedReader(new FileReader("file_name")));
   while (s.hasNext())
      System.out.println(s.next());
   } catch (IOException e)
   {
      // Do the error stuff.
      e.printStackTrace();
   } finally
   {
      // Do it anyway. If error happens or not.
      if (s != null)
         s.close();
   }
}

并且 throws IOException 放置在错误的位置,应该放置在这里:

public static void main(String[] args) throws IOException {

在这种情况下,您不需要任何 try、catch 块 - 您只需将该异常传递给覆盖方法(在您的情况下,您不需要担心它)以让它处理抛出的异常,但如果您想使用 try、catch 块处理异常,则不需要那。

This is the part You got it totally wrong.

try
{
   s = new Scanner((Readable) new BufferedReader(new FileReader("file_name")));
   while (s.hasNext())
      System.out.println(s.next());
   } catch (IOException e)
   {
      // Do the error stuff.
      e.printStackTrace();
   } finally
   {
      // Do it anyway. If error happens or not.
      if (s != null)
         s.close();
   }
}

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.

2025-01-09 15:51:54

throws 子句在方法声明中有效,但在方法体内无效。

throws clause is valid on the method declaration and not inside the method body.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文