我应该在Java的哪里添加IOExceptions?

时间:2020-04-11 07:22:02

标签: java ioexception

我有两个具有以下代码的文件:

文件:Testing.java

import java.io.*;

public class Testing {
    public static void main(String args[]) throws IOException {
        new Testing();
    }
    public Testing() throws IOException {
        new ReadBytes();
    }
}

文件:ReadBytes.Java

import java.io.*;
public class ReadBytes {
    public ReadBytes () throws IOException {
        byte data[] = new byte[20];

        System.out.println("Ingrese alguinos caracteres");
        System.in.read(data);
        System.out.println("Usted has escrito: ");
        for (int i=0; i<data.length;i++){
            System.out.println((char)data[i]);
        }
    }
}

当我第一次尝试此代码时,我只将IOException扔到了ReadBytes类的构造函数上,以为这是最合适的地方,因为这将是该类一些IO。但是,在编译期间,IDE抱怨它也应该在两个位置添加到Testing.java文件中:

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

如果已经有一个要处理IOException的类,为什么还要在其他情况下抛出IOExceptions?

0 个答案:

没有答案