|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.Arrays; |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +public class martinbohorquez { |
| 6 | + public static void main(String[] args) { |
| 7 | + try { |
| 8 | +// int num = 10 / 0; |
| 9 | + List<Integer> lista = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); |
| 10 | + System.out.println(lista.get(6)); |
| 11 | + } catch (ArithmeticException e) { |
| 12 | +// e.printStackTrace(); |
| 13 | + System.out.printf("No puede dividirse por cero: %s%n", e.getMessage()); |
| 14 | + } catch (IndexOutOfBoundsException e) { |
| 15 | + System.out.printf("Indice fuera de límites: %s%n", e.getMessage()); |
| 16 | + } |
| 17 | + System.out.println("Continuando con el programa...!"); |
| 18 | + |
| 19 | + /* |
| 20 | + * DIFICULTAD EXTRA |
| 21 | + */ |
| 22 | + processParams(Arrays.asList(10, 0, 30)); |
| 23 | + } |
| 24 | + |
| 25 | + private static void processParams(List<Integer> lista) { |
| 26 | + List<Integer> list = new ArrayList<>(lista); |
| 27 | + try { |
| 28 | + System.out.printf("La división es: %d%n", (list.get(2) / list.get(0))); |
| 29 | + System.out.println(list.get(2)); |
| 30 | + if (list.get(1) == 0) throw new MartinException("elemento con valor zero!"); |
| 31 | + } catch (ArithmeticException e) { |
| 32 | + System.out.printf("No puede dividirse por cero: %s%n", e.getMessage()); |
| 33 | + } catch (IndexOutOfBoundsException e) { |
| 34 | + System.out.printf("Indice fuera de límites: %s%n", e.getMessage()); |
| 35 | + } catch (MartinException e) { |
| 36 | + System.out.printf("Se ha producido un error personalizado: %s%n", e.getMessage()); |
| 37 | + } catch (Exception e) { |
| 38 | + System.out.printf("Se ha producido un error inesperado: %s%n", e.getMessage()); |
| 39 | + } finally { |
| 40 | + System.out.println("Programa finalizado!"); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private static class MartinException extends Exception { |
| 45 | + public MartinException(String message) { |
| 46 | + super(message); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments