Posible solución Excepciones Ejercicio 2

De MediaWiki
Revisión del 20:12 9 ene 2018 de Wiki (discusión | contribuciones) (Página creada con «'''Clase LimitesException''' ::<syntaxhighlight lang="java" line enclose="div" highlight="" > package EjerciciosPOO.excepciones.ejercicio2; →‎* * * @author clase: pub...»)
(dif) ← Revisión anterior | Revisión actual (dif) | Revisión siguiente → (dif)
Ir a la navegación Ir a la búsqueda

Clase LimitesException

 1 package EjerciciosPOO.excepciones.ejercicio2;
 2 
 3 /**
 4  *
 5  * @author clase
 6  */
 7 public class LimitesException extends Exception {
 8     
 9     public LimitesException(String msg){
10         super(msg);
11     }
12     
13 }


Clase Lectura

 1 /*
 2  * To change this license header, choose License Headers in Project Properties.
 3  * To change this template file, choose Tools | Templates
 4  * and open the template in the editor.
 5  */
 6 package EjerciciosPOO.excepciones.ejercicio2;
 7 
 8 import java.util.InputMismatchException;
 9 import java.util.NoSuchElementException;
10 import java.util.Scanner;
11 
12 /**
13  *
14  * @author clase
15  */
16 public class Lectura {
17     private static Scanner sc=new Scanner(System.in);
18     
19     
20     public static byte leerEntero() throws LimitesException,Exception{  
21         System.out.println("Introduce un número:");
22         int numero = sc.nextInt();  // Usamos nextInt para que no lance una excepcion si el número es mayor que 255
23         if (numero<1 || numero > 100){
24             throw new LimitesException("Fuera de los límites");
25         }
26         sc.nextLine();  // Para que lee el salto de línea
27         return (byte)numero;
28     }
29     public static String leerCadena() throws InputMismatchException,NoSuchElementException,Exception{ // Exception la usaremos en caso de que envíe la cadena vacía. No se recomienda utilizar el tipo de excepción 'general'
30         System.out.println("Introduce una cadena:");        // InputMismatchException,NoSuchElementException: Derivan de RunTimeException, por lo tanto son de tipo unchecked 
31         String cadena = sc.nextLine();
32         
33         if (cadena.length()==0){
34             throw new Exception("Longitud cadena igual a cero");
35         }
36         
37         return cadena;
38         
39     }
40     
41     public static boolean leerBooleano() throws InputMismatchException,NoSuchElementException, IllegalStateException { // Derivan de RunTimeException, por lo tanto son de tipo unchecked
42         System.out.println("Introduce un booleano:");
43         
44         boolean dato = sc.nextBoolean();
45         return dato;
46     }
47 }


Clase Principal

 1 /*
 2  * To change this license header, choose License Headers in Project Properties.
 3  * To change this template file, choose Tools | Templates
 4  * and open the template in the editor.
 5  */
 6 package EjerciciosPOO.excepciones.ejercicio2;
 7 
 8 import java.util.InputMismatchException;
 9 import java.util.NoSuchElementException;
10 import java.util.logging.Level;
11 import java.util.logging.Logger;
12 
13 /**
14  *
15  * @author clase
16  */
17 public class Principal {
18     
19     public static void main(String[] arg){
20         
21         try {
22             byte numero = Lectura.leerEntero();
23         } catch (LimitesException ex) {
24             System.out.println("Error en los límetes");
25         }
26         catch (Exception e) {
27             System.out.println("Error general");
28         }
29         
30         try {
31             String cadena = Lectura.leerCadena();
32         } catch (InputMismatchException ex) {
33             Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
34         } catch (NoSuchElementException ex) {
35             Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
36         } catch (Exception ex) {
37             Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
38         }
39 
40         try {
41             boolean valor = Lectura.leerBooleano();
42         } catch (InputMismatchException ex) {
43             Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
44         } catch (NoSuchElementException ex) {
45             Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
46         } catch (IllegalStateException ex) {
47             Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
48         }
49 
50     }
51     
52 }






-- Ángel D. Fernández González -- (2017).