-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_exceptions.py
More file actions
54 lines (40 loc) · 1.05 KB
/
12_exceptions.py
File metadata and controls
54 lines (40 loc) · 1.05 KB
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
# Exception Handling
# try except
number_tree = 5
number_four = 1
number_four = "1"
try:
print(number_tree + number_four)
print("No se ha producido un error")
except:
print("Se ha producido un error")
# capturando errores por su tipo
num_1 = "1"
num_2 = 2
try:
print(num_1 + num_2)
print("Se ejecuta el try si el programa va bien")
except TypeError:
print("Se ejecuta si hay un TypeError")
except ValueError:
print("Se ejecuta si hay un ValueError")
# Capturando el error
num_3 = 30
num_4 = 0
try:
print(num_3 / num_4)
print("Si está todo bien voy yo")
except ZeroDivisionError as error:
print("Si alguien intenta dividir por 0 voy yo")
print(error)
print(f"Ha ocurrido un un error del tipo {error}")
# Usando un manejador que recibe todos los tipos de errores
num_5 = 3436
num_6 = 0
try:
print(num_5 / num_6)
print("Se ejecuta si no hay error")
except ValueError as e:
print("Se ejecuta si hay un ValueError")
except Exception as e:
print("Se ejecuta si hay algún error que no he declarado")