Skip to content

Commit d2a7521

Browse files
authored
Merge pull request #2217 from N0HagoNada/main
#10 - c++
2 parents 1cfb135 + 9aa6fc2 commit d2a7521

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

0 commit comments

Comments
 (0)