public class Flujo {
public static void main(String[] args) {
System.out.println("Inicio main");
metodo1();
System.out.println("Fin main");
}
private static void metodo1() {
System.out.println("Inicio metodo1");
metodo2();
System.out.println("Fin metodo1");
}
private static void metodo2() {
System.out.println("Inicio metodo2");
for(int i = 1; i <= 5; i++) {
System.out.println(i);
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) { // atrapa el error
System.out.println("Atrapo Excepcion");
System.out.println(exception.getMessage());
exception.printStackTrace();
}
}
System.out.println("Fin metodo2");
}
} esto me arrojo estara bien Inicio main Inicio metodo1 Inicio metodo2 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:29) at Flujo.metodo1(Flujo.java:12) at Flujo.main(Flujo.java:6) 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:29) at Flujo.metodo1(Flujo.java:12) at Flujo.main(Flujo.java:6) 3 Atrapo Excepcion / by zero java.lang.ArithmeticException: / by zero at Flujo.metodo2(Flujo.java:24) at Flujo.metodo1(Flujo.java:12) at Flujo.main(Flujo.java:6) 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:29) at Flujo.metodo1(Flujo.java:12) at Flujo.main(Flujo.java:6) 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:29) at Flujo.metodo1(Flujo.java:12) at Flujo.main(Flujo.java:6) Fin metodo2 Fin metodo1 Fin main