File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
Roadmap/10 - EXCEPCIONES/python Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ ''' Try/ Except /Finally
2+
3+ # Try / Except / Finally es una estructura que permite manejar errores
4+ # en tiempo de ejecución, es decir, cuando el programa ya está en ejecución.
5+ '''
6+
7+ def division (x : float , y : float ):
8+ return x / y
9+
10+ try :
11+ resultado = division (10 , 0 )
12+ print (resultado )
13+ except Exception as e :
14+ print (f"Error: { e } " )
15+ finally :
16+ print ("Fin del programa" )
17+
18+ # Ejemplo
19+ # En este ejemplo se muestra como se puede manejar un error en tiempo de ejecución
20+
21+ def saludo (x : str ):
22+ return "Hola " + x
23+
24+ try :
25+ hola = saludo (2 )
26+ print (hola )
27+ except Exception as e :
28+ print (f"Error: { e } " )
29+ finally :
30+ print ("Fin del programa" )
31+
32+ def contraseña (x : str ):
33+ if len (x ) < 10 :
34+ raise ValueError ("La contraseña debe tener más de 9 caracteres" )
35+
36+ ''' Extra '''
37+ class Ortography (Exception ):
38+ def __init__ (self , text : str ):
39+ self .text = text
40+
41+ def oracion (x : str ):
42+ if x [- 1 ] != "." :
43+ raise Ortography ("El texto no termina en punto" )
44+ if (x .isnumeric ):
45+ raise Ortography ("El texto no puede ser un número" )
46+ if len (x ) < 1 :
47+ raise Ortography ("El texto no puede estar vacío" )
48+
49+ try :
50+ oracion (0 / 0 )
51+ except Exception as e :
52+ print ("Revisar la info del problema" )
53+ finally :
54+ print ("Fin del programa" )
You can’t perform that action at this time.
0 commit comments