File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Roadmap/10 - EXCEPCIONES/c++ Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < vector>
3
+ #include < stdexcept>
4
+
5
+ double division (double a, double b)
6
+ {
7
+ if (b == 0 )
8
+ {
9
+ throw std::runtime_error (" Error: División por cero" );
10
+ }
11
+ return a / b;
12
+ }
13
+
14
+ int obtenerValorIndice (const std::vector<int > &numeros, int indice)
15
+ {
16
+ if (indice < 0 || indice >= static_cast <int >(numeros.size ()))
17
+ {
18
+ throw std::out_of_range (" Error: Índice fuera de rango" );
19
+ }
20
+ return numeros[indice];
21
+ }
22
+
23
+ int main ()
24
+ {
25
+ try
26
+ {
27
+ double resultado = division (10 , 0 );
28
+ std::cout << " Resultado: " << resultado << std::endl;
29
+ }
30
+ catch (const std::runtime_error &e)
31
+ {
32
+ std::cerr << e.what () << std::endl;
33
+ }
34
+
35
+ try
36
+ {
37
+ std::vector<int > numeros = {1 , 2 , 3 };
38
+ int valor = obtenerValorIndice (numeros, 5 );
39
+ std::cout << " Valor: " << valor << std::endl;
40
+ }
41
+ catch (const std::out_of_range &e)
42
+ {
43
+ std::cerr << e.what () << std::endl;
44
+ }
45
+
46
+ std::cout << " Fin del programa" << std::endl;
47
+ return 0 ;
48
+ }
You can’t perform that action at this time.
0 commit comments