Catch Java no captura
Publicado por gin (2 intervenciones) el 25/04/2020 22:34:36
Buenas tardes;
Buenas tardes, estoy practicando array y try catch, pero no todos los catch funcionan, solo el: java.util.InputMismatchException
Cuando ingreso un negativo o 0 no se captura, no entra a NegativeArraySizeException ex ni a ArithmeticException ex:
Buenas tardes, estoy practicando array y try catch, pero no todos los catch funcionan, solo el: java.util.InputMismatchException
Cuando ingreso un negativo o 0 no se captura, no entra a NegativeArraySizeException ex ni a ArithmeticException ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.Scanner;
public class arreglo_Practica{
public static void main(String[] args) {
Scanner leerNum = new Scanner(System.in);
/* int[] uno = {8823, 443, 662, 1, 77, 98, 323, 5, 2932, 51};
int[] dos = {9, 453, 1749, 27, 511, 4, 1980, 42, 71, 35};
int[] tres = new int[10];*/
int opc=0, i=0, j=0, k=0, l=0, aux=0;
boolean flag = true;
do{
try{
int[] uno = {8823, 443, 662, 1, 77, 98, 323, 5, 2932, 51};
int[] dos = {9, 453, 1749, 27, 511, 4, 1980, 42, 71, 35};
int[] tres = new int[10];
System.out.println("\n - MENÚ -");
System.out.println("1.- Primer digito del elemento.");
System.out.println("2.- Intercambiar elementos.");
System.out.println("3.- Sumar Arreglos.");
System.out.println("4.- SALIR.\n");
System.out.print("Elige tu opción: ");
opc = leerNum.nextInt();
System.out.println("");
/*if(opc <= 0){
System.out.println("¡Ingresaste un número negativo, ingrese una opción 1-4 del menú!");
flag=false;
}*/
switch(opc){
case 1:
for (i = 0; i < uno.length; i++){
String numero = String.valueOf(uno[i]);
aux = Integer.parseInt(numero.substring(0,1));
System.out.print("["+String.valueOf(aux)+"] ");
}
break;
case 2:
System.out.println("");
for (i = 0; i < tres.length; i++){
tres[i]=dos[i];
}
for (i = 0; i < dos.length; i++){
dos[i]=uno[i];
}
for (i = 0; i < uno.length; i++){
uno[i]=tres[i];
}
System.out.println("- Vector I (intercambiado) con Vector II -\n");
for (j = 0; j < uno.length; j++){
System.out.print("["+uno[j]+"] ");
}
System.out.println("\n");
System.out.println("- Vector II (intercambiado) con Vector I -\n");
for (k = 0; k < dos.length; k++){
System.out.print("["+dos[k]+"] ");
}
break;
case 3:
for(i = 0; i < uno.length; i++){
tres[i]= uno[uno.length - 1 - i] + dos[i];
}
System.out.println("El resultado de la 'Suma Cruzada' entre Vector I y Vector II es:\n");
for (l = 0; l < tres.length ; l++) {
System.out.print("["+tres[l]+"] ");
}
System.out.println("");
break;
case 4:
System.out.println("");
break;
}
System.out.println("");
}catch(java.util.InputMismatchException ex){
System.err.println("¡Valor inválido ingrese una opción 1-4 del menú!");
flag= false;
}catch(NegativeArraySizeException ex){
System.err.println("¡Ingresaste un número negativo, ingrese una opción 1-4 del menú!");
flag = false;
}catch(ArithmeticException ex){
System.err.println("¡Ingresaste un '0', ingrese una opción 1-4 del menú!");
flag = false;
}catch(NumberFormatException ex){
System.err.println("Error Desconocido!!"+ex.toString());
flag = false;
}
leerNum = new Scanner(System.in);
}while(opc!=4);
System.out.println("- FIN -");
}
}
Valora esta pregunta


0