package com.codemonkey;
/**
* @author Fer Carraro
* @date 01/05/2018
*/
import static java.lang.System.out;
import static java.lang.System.err;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
//Main.java
//Compilar: javac Main.java
//Ejecutar: java Main
public class Main{
public static void main(String[] args) {
int division=0;
out.println("Excepciones:");
try{
division =3/0;
}catch(ArithmeticException | NullPointerException e1){
err.println("Causa:"+e1.getCause()+" - Mensaje:"+e1.getMessage());//Excepcion de tipo ArithmeticException
e1.printStackTrace();
}
try{
division = 4/0;
}catch(NullPointerException | ArithmeticException e2){
err.println("Causa:"+e2.getCause()+" - Mensaje:"+e2.getMessage());//Excepcion de tipo ArithmeticException
e2.printStackTrace();
}
int[] vector = new int[2];//tam=2, indices [0,1]
try{
vector[0]= 2;
vector[1]= 4;
vector[2]= 5;
}catch(ArrayIndexOutOfBoundsException | NullPointerException e3){
err.println("Causa:"+e3.getCause()+" - Mensaje:"+e3.getMessage());//Excepcion de tipo ArrayIndexOutOfBoundsException
e3.printStackTrace();
}
try{
vector[0]= 10;
vector[1]= 8;
vector[2]= 2;
}catch(NullPointerException | ArrayIndexOutOfBoundsException e4){
err.println("Causa:"+e4.getCause()+" - Mensaje:"+e4.getMessage());//Excepcion de tipo ArrayIndexOutOfBoundsException
e4.printStackTrace();
}
File file = new File("/home/fer/Documents/noExiste.dat");
BufferedReader br = null;
try{
FileReader fr = new FileReader(file);
br = new BufferedReader(fr);
String cad;
while((cad=br.readLine())!=null){
out.println(cad);
}
br.close();
}catch(java.io.IOException | NullPointerException e5){////Excepcion de tipojava.io.IOException, FileNotFoundException
err.println("Error leyendo el archivo:");
e5.printStackTrace();
}
try {
java.io.LineNumberReader lineReader = new java.io.LineNumberReader(new FileReader("/home/fer/Documents/noExiste.dat"));
String line = lineReader.readLine();
lineReader.close();
out.println(line);
}catch (FileNotFoundException fne) {
err.println("Archivo no existente");//Esto se ejecutara, el archivo no existe
}catch (java.io.IOException ioe) {
err.println("Error leyendo el archivo");
}catch(Exception ex){
err.println("Ha ocurrido una excepcion");
}
}
}
Salida del programa:
Excepciones:
Causa:null - Mensaje:/ by zero
java.lang.ArithmeticException: / by zero
at com.codemonkey.Main.main(Main.java:25)
Causa:null - Mensaje:/ by zero
java.lang.ArithmeticException: / by zero
at com.codemonkey.Main.main(Main.java:31)
Causa:null - Mensaje:2
java.lang.ArrayIndexOutOfBoundsException: 2
at com.codemonkey.Main.main(Main.java:41)
Causa:null - Mensaje:2
java.lang.ArrayIndexOutOfBoundsException: 2
at com.codemonkey.Main.main(Main.java:50)
Error leyendo el archivo:
java.io.FileNotFoundException: /home/fer/Documents/noExiste.dat (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:196)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:139)
at java.base/java.io.FileReader.<init>(FileReader.java:72)
at com.codemonkey.Main.main(Main.java:59)
Archivo no existente
Excepciones:
Causa:null - Mensaje:/ by zero
java.lang.ArithmeticException: / by zero
at com.codemonkey.Main.main(Main.java:25)
Causa:null - Mensaje:/ by zero
java.lang.ArithmeticException: / by zero
at com.codemonkey.Main.main(Main.java:31)
Causa:null - Mensaje:2
java.lang.ArrayIndexOutOfBoundsException: 2
at com.codemonkey.Main.main(Main.java:41)
Causa:null - Mensaje:2
java.lang.ArrayIndexOutOfBoundsException: 2
at com.codemonkey.Main.main(Main.java:50)
Error leyendo el archivo:
java.io.FileNotFoundException: /home/fer/Documents/noExiste.dat (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:196)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:139)
at java.base/java.io.FileReader.<init>(FileReader.java:72)
at com.codemonkey.Main.main(Main.java:59)
Archivo no existente
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.