Search code examples
javaunreachable-code

Why are the last two instructions unreachable code?


Eclipse spots them as unreachable code, which apparently is a code that will never be read for there's no path to reach it, but I don't see why. The instructions are inside a main() method

    //leemos
    FileInputStream fis;
    ObjectInputStream ois;
    Alumno alumnoLeido = null;
    String cadena ="";
    JTextArea area = new JTextArea(6,1);
    while(true){
        try {
            fis = new FileInputStream("alumnos.txt");
            ois = new ObjectInputStream(fis);
            alumnoLeido = (Alumno) ois.readObject();
            ois.close();
            cadena = "Alumno " + alumnoLeido.getNombre() + " " + alumnoLeido.getApellido() + " vive en " 
            + alumnoLeido.getDireccion() + " y tiene una beca de " + alumnoLeido.getBeca() + " euros \r\n";
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    area.append(cadena);
    JOptionPane.showMessageDialog(null, area, "Alumnos",1);

Solution

  • while(true) is an infinite loop. Without a break, the loop will never terminate and allow the following code to execute. So, you will never reach the remaining statements.