|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | + |
| 4 | +public class Josegs95 { |
| 5 | + public static void main(String[] args) { |
| 6 | + //Excepciones |
| 7 | + try{ |
| 8 | + int a = 10 / 0; //Comentar esta línea para comprobar el caso de abajo |
| 9 | + |
| 10 | + List<Object> list = new ArrayList<>(); |
| 11 | + list.get(8); |
| 12 | + } catch (Exception e){ |
| 13 | + System.out.println("Se ha producido un error: " + e.getMessage()); |
| 14 | + } |
| 15 | + |
| 16 | + //Reto |
| 17 | + System.out.println("\n"); |
| 18 | + retoFinal(); |
| 19 | + } |
| 20 | + |
| 21 | + public static void retoFinal(){ |
| 22 | + Object[] params = {"git", "commit", "-m", "'#10 - Java'"}; |
| 23 | + //Object[] params = {"git", "commit", "-m", 85}; |
| 24 | + //Object[] params = {"ls", "commit", "-m", "'#10 - Java'"}; |
| 25 | + boolean error = false; |
| 26 | + |
| 27 | + try{ |
| 28 | + |
| 29 | + processParams(params); |
| 30 | + } catch (IndexOutOfBoundsException e) { |
| 31 | + System.out.println("El número de parametros debe ser de 4 o mas."); |
| 32 | + error = true; |
| 33 | + } catch (ClassCastException e) { |
| 34 | + System.out.println("El elemento número 4 deber ser una cadena de texto"); |
| 35 | + error = true; |
| 36 | + } catch (NotRightCommandException e) { |
| 37 | + System.out.println("El elemento número 1 deber ser el comando 'git'"); |
| 38 | + error = true; |
| 39 | + } catch (Exception e) { |
| 40 | + System.out.println("Se ha producido un error inesperado: " + e.getMessage()); |
| 41 | + error = true; |
| 42 | + } finally { |
| 43 | + if (!error) |
| 44 | + System.out.println("El programa no ha tenido ningún error"); |
| 45 | + System.out.println("El programa finaliza."); |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + private static void processParams(Object[] params) throws Exception{ |
| 51 | + if (params.length < 4) |
| 52 | + throw new IndexOutOfBoundsException(); |
| 53 | + |
| 54 | + if (!(params[3] instanceof String)){ |
| 55 | + throw new ClassCastException(); |
| 56 | + } |
| 57 | + |
| 58 | + if (!params[0].toString().toLowerCase().equals("git")) |
| 59 | + throw new NotRightCommandException(); |
| 60 | + |
| 61 | + String message = (String) params[3]; |
| 62 | + System.out.println("El mensaje del commit es: " + message); |
| 63 | + } |
| 64 | + |
| 65 | + public static class NotRightCommandException extends Exception{ |
| 66 | + |
| 67 | + } |
| 68 | +} |
0 commit comments