|
| 1 | +package basic.c09_exceptions; |
| 2 | + |
| 3 | +/* |
| 4 | +Clase 8 - Manejo de excepciones, depuración y extras (28/05/2025) |
| 5 | +Vídeo: https://www.twitch.tv/videos/2471305243 |
| 6 | +*/ |
| 7 | + |
| 8 | +public class Exceptions { |
| 9 | + |
| 10 | + public static void main(String[] args) { |
| 11 | + |
| 12 | + // Manejo de excepciones |
| 13 | + |
| 14 | + // try catch |
| 15 | + try { |
| 16 | + var result = 10 / 0; |
| 17 | + System.out.println(result); |
| 18 | + } catch (ArithmeticException e) { |
| 19 | + System.out.println("Error: " + e); |
| 20 | + } |
| 21 | + |
| 22 | + // try con múltiples catch |
| 23 | + try { |
| 24 | + var result = 10 / 5; |
| 25 | + System.out.println(result); |
| 26 | + |
| 27 | + var name = "Brais"; |
| 28 | + name = null; |
| 29 | + System.out.println("Name: " + name.toUpperCase()); |
| 30 | + } catch (ArithmeticException e) { |
| 31 | + System.out.println("Cuidado con dividir algo que no puedes: " + e); |
| 32 | + } catch (NullPointerException e) { |
| 33 | + System.out.println("Ha ocurrido un null pointer mítico!"); |
| 34 | + } catch (Exception e) { |
| 35 | + System.out.println("Se ha producido un error no esperado"); |
| 36 | + } |
| 37 | + |
| 38 | + // finally |
| 39 | + try { |
| 40 | + var result = 10 / 0; |
| 41 | + System.out.println(result); |
| 42 | + } catch (ArithmeticException e) { |
| 43 | + System.out.println("Error: " + e); |
| 44 | + } finally { |
| 45 | + System.out.println("Fin del bloque try-catch"); |
| 46 | + } |
| 47 | + |
| 48 | + // throw |
| 49 | + var throwExample = new ThrowExample(); |
| 50 | + try { |
| 51 | + throwExample.checkAge(15); |
| 52 | + } catch (IllegalArgumentException e) { |
| 53 | + System.out.println("Error revisando la edad: " + e.getMessage()); |
| 54 | + } |
| 55 | + |
| 56 | + // Excepción personalizada |
| 57 | + try { |
| 58 | + throwExample.checkScore(450); |
| 59 | + } catch (CustomException e) { |
| 60 | + System.out.println("Error revisando la puntuación: "+ e.getMessage()); |
| 61 | + } |
| 62 | + |
| 63 | + System.out.println("Fin"); |
| 64 | + } |
| 65 | +} |
0 commit comments