public class Flujo {
public static void main(String[] args) {
System.out.println("Inicio main");
metodo1();
System.out.println("Fin de main");
}
public static void metodo1(){
System.out.println("Inicio de metodo 1");
metodo2();
System.out.println("fin de metodo 1");
}
public static void metodo2(){
System.out.println("Inicio metodo 2");
for(int i =1; i<= 5; i++){
System.out.println(i);
// try no existe sin cath
try{ //intenta esto
if (i == 3){
int num = 0;
int resultado = i/num;
System.out.println(resultado);
}
String test = null;
System.out.println(test.toString());
}catch(ArithmeticException | NullPointerException exception){
System.out.println("Atrapo excepcion");
System.out.println(exception.getMessage());// Detalle del mensaje
exception.printStackTrace();//nos da los que paso
}
}
System.out.println("Fin de metodo2");
}
}
Inicio main
Inicio de metodo 1
Inicio metodo 2
1
Atrapo excepcion
Cannot invoke "String.toString()" because "test" is null
java.lang.NullPointerException: Cannot invoke "String.toString()" because "test" is null
at Flujo.metodo2(Flujo.java:24)
at Flujo.metodo1(Flujo.java:9)
at Flujo.main(Flujo.java:4)
2
Atrapo excepcion
Cannot invoke "String.toString()" because "test" is null
java.lang.NullPointerException: Cannot invoke "String.toString()" because "test" is null
at Flujo.metodo2(Flujo.java:24)
at Flujo.metodo1(Flujo.java:9)
at Flujo.main(Flujo.java:4)
3
Atrapo excepcion
/ by zero
java.lang.ArithmeticException: / by zero
at Flujo.metodo2(Flujo.java:20)
at Flujo.metodo1(Flujo.java:9)
at Flujo.main(Flujo.java:4)
4
Atrapo excepcion
Cannot invoke "String.toString()" because "test" is null
java.lang.NullPointerException: Cannot invoke "String.toString()" because "test" is null
at Flujo.metodo2(Flujo.java:24)
at Flujo.metodo1(Flujo.java:9)
at Flujo.main(Flujo.java:4)
5
Atrapo excepcion
Cannot invoke "String.toString()" because "test" is null
java.lang.NullPointerException: Cannot invoke "String.toString()" because "test" is null
at Flujo.metodo2(Flujo.java:24)
at Flujo.metodo1(Flujo.java:9)
at Flujo.main(Flujo.java:4)
Fin de metodo2
fin de metodo 1
Fin de main