Descripción
Constructor que nos permite crear una excepción IOException.
Sintaxis
public IOException()
public IOException(String message)
public IOException(String message, Throwable cause)
public IOException(Throwable cause)
Parámetros
- Throwable cause, Elemento que permite asociar cualquier excepción ya que
Throwable
es la superclase de todas las excepciones. - String message, Mensaje que se quiere asociar a la
IOException
Clase Padre
Ejemplo
import java.io.*;
public class PersonalizarIOException {
public static long numeroLineas(String fichero) throws IOException {
long lNumeroLineas = 0;
try{
FileReader file = new FileReader(fichero);
BufferedReader bf = new BufferedReader(file);
while ((bf.readLine())!=null) {
lNumeroLineas++;
}
bf.close();
}
catch(IOException ioe){
throw new IOException("Error en la lectura del fichero",ioe);
}
return lNumeroLineas;
}
public static void main(String[] args) {
String fichero = "datos.txt";
try {
numeroLineas(fichero);
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
ioe.printStackTrace();
}
}
}
Artículos
Vídeos Java
Disfruta también de nuestros artículos sobre Java en formato vídeo. Aprovecha y suscribete a nuestro canal.