Skip to content

Commit d2c055a

Browse files
authored
Merge pull request mouredev#6890 from danhingar/ejercicio10
#10 - Java
2 parents 138298e + 393cff7 commit d2c055a

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.List;
2+
3+
public class danhingar {
4+
5+
public static void main(String[] args) {
6+
System.out.println(divide(10, 0));
7+
List<String> list = List.of("A","B","C");
8+
System.out.println(search(list,4));
9+
10+
try {
11+
processParams(List.of(1,4,"a"));
12+
System.out.println("No se ha producido ningún error.");
13+
} catch (IndexOutOfBoundsException e) {
14+
System.out.println("El número de elementos de la lista debe ser mayor que dos.");
15+
} catch (ArithmeticException e){
16+
System.out.println("El segundo elemento de la lista no puede ser un cero.");
17+
}catch(StrTypeError e){
18+
System.out.println(e);
19+
}catch(Exception e){
20+
System.out.println("Se ha producido un error inesperado: "+e);
21+
}finally{
22+
System.out.println("El programa finaliza sin detenerse.");
23+
}
24+
}
25+
26+
private static Integer divide(int number1, int number2){
27+
try {
28+
return number1/number2;
29+
} catch (Exception e) {
30+
System.out.println("No se ha podido realizar la división");
31+
return null;
32+
}
33+
}
34+
35+
private static String search(List<String> list,Integer index) {
36+
try {
37+
return list.get(index);
38+
} catch (Exception e) {
39+
System.out.println("No se ha podido recuperar el elemento del ídice :"+index);
40+
return null;
41+
}
42+
}
43+
44+
//EXTRA
45+
private static void processParams(List<Object> numbers) throws danhingar.StrTypeError{
46+
if(numbers.size()<3){
47+
throw new IndexOutOfBoundsException();
48+
}else if(numbers.get(1).equals(0)){
49+
throw new ArithmeticException();
50+
}else if(numbers.get(2).getClass().equals(String.class)){
51+
throw new StrTypeError("El tercer elemento no puede ser una cadena de texto.");
52+
}
53+
}
54+
55+
private static class StrTypeError extends Exception {
56+
57+
public StrTypeError(String message){
58+
super(message);
59+
}
60+
61+
}
62+
63+
}

0 commit comments

Comments
 (0)