File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Roadmap/10 - EXCEPCIONES/python Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ def div_0 ():
2
+ while True :
3
+ n = int (input ('ingrese un numero: ' ))
4
+ if n == 0 :
5
+ break
6
+ try :
7
+ print (n / 0 )
8
+ except ZeroDivisionError as e : print (f'Error: { e } ' )
9
+
10
+ div_0 ()
11
+
12
+
13
+ '''
14
+ EXTRA
15
+ '''
16
+
17
+ # Definición de una excepción personalizada
18
+ class MiExcepcionPersonalizada (Exception ):
19
+ def __init__ (self , mensaje ):
20
+ super ().__init__ (mensaje )
21
+
22
+
23
+ def procesar_parametros (parametro ):
24
+ try :
25
+ if parametro == 1 :
26
+ raise ValueError ("¡Este es un valor incorrecto!" )
27
+ elif parametro == 2 :
28
+ raise IndexError ("¡Índice fuera de rango!" )
29
+ elif parametro == 3 :
30
+ raise MiExcepcionPersonalizada ("¡Esto es una excepción personalizada!" )
31
+ else :
32
+ print ("Parámetro válido:" , parametro )
33
+ except ValueError as ve :
34
+ print (f"Error: { ve } \n Tipo: { type (ve ).__name__ } " )
35
+ except IndexError as ie :
36
+ print (f"Error: { ie } \n Tipo: { type (ie ).__name__ } " )
37
+ except MiExcepcionPersonalizada as me :
38
+ print (f"Error: { me } \n Tipo: { type (me ).__name__ } " )
39
+ else :
40
+ print ("No se ha producido ningún error." )
41
+ finally :
42
+ print ("La ejecución ha finalizado." )
43
+
44
+ try :
45
+ procesar_parametros (0 )
46
+ procesar_parametros (1 )
47
+ procesar_parametros (2 )
48
+ procesar_parametros (3 )
49
+ procesar_parametros ()
50
+ except Exception as e :
51
+ print ("Tipo de error:" , type (e ).__name__ )
You can’t perform that action at this time.
0 commit comments