0

I have two files with the following code:

File: Testing.java

import java.io.*;

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

File: 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]);
        }
    }
}

When I first tried this code, I've only throwed IOException on the ReadBytes class constructor thinking that this would be the most appropriate place to do it since is the class that is going to have some IO. However, during compilation, the IDE complained that it should also be added to the Testing.java file in two places:

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

If there is already a class that is going to handle the IOException, why do I have to throw IOExceptions on the other clases?

0 Answers0