132 lines
3.9 KiB
Markdown
132 lines
3.9 KiB
Markdown
|
# Errores y Excepciones
|
||
|
|
||
|
- [Error de sintaxis \*SyntaxError\*](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#error-de-sintaxis)
|
||
|
- [Error de tipo \*TypeError\*](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#error-de-tipo)
|
||
|
- [Error de importación \*ModuleNotFoundError\*](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#error-de-importaci%C3%B3n)
|
||
|
- [Error de nombre \*NameError\*](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#error-de-nombre)
|
||
|
- [Error archivo no encontrado \*FileNotFoundError\*](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#error-archivo-no-encontrado)
|
||
|
- [Error de valor \*ValueError\*](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#error-de-valor)
|
||
|
- [Error de indice \*IndexError\*](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#error-de-indice)
|
||
|
- [Error de llave \*KeyError\*](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#error-de-llave)
|
||
|
- [Levantar una excepción](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#levantar-una-excepci%C3%B3n)
|
||
|
- [Assert](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#assert)
|
||
|
- [Manejo de excepciones (try, except, else, finally)](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#manejo-de-excepciones)
|
||
|
- [Definir excepciones y clases de error propias.(BaseException subClass)](https://gitea.kickto.net/devfzn/Apuntes_Python/src/branch/master/02_conceptos/09_exceptions_and_errors#definir-excepciones-propias)
|
||
|
|
||
|
### Error de sintaxis
|
||
|
```python
|
||
|
a = 5 print(a)
|
||
|
SyntaxError: invalid syntax
|
||
|
|
||
|
print('hola'))
|
||
|
SyntaxError: unmatched ')'
|
||
|
```
|
||
|
|
||
|
### Error de tipo
|
||
|
```python
|
||
|
a = 5 + 'hola'
|
||
|
TypeError: unsupported operand type(s) for +: 'int' and 'str'
|
||
|
```
|
||
|
|
||
|
### Error de importación
|
||
|
```python
|
||
|
import supermodulo
|
||
|
ModuleNotFoundError: No module named 'supermodulo'
|
||
|
```
|
||
|
|
||
|
### Error de nombre
|
||
|
```python
|
||
|
a = b
|
||
|
NameError: name 'b' is not defined
|
||
|
```
|
||
|
|
||
|
### Error archivo no encontrado
|
||
|
```python
|
||
|
f = open('algun_archivo')
|
||
|
FileNotFoundError: [Errno 2] No such file or directory: 'algun_archivo'
|
||
|
```
|
||
|
|
||
|
### Error de valor
|
||
|
```python
|
||
|
a = [1,2,3]
|
||
|
a.remove(4)
|
||
|
ValueError: list.remove(x): x not in list
|
||
|
```
|
||
|
|
||
|
### Error de indice
|
||
|
```python
|
||
|
a = [1,2,3]
|
||
|
a[3]
|
||
|
IndexError: list index out of range
|
||
|
```
|
||
|
|
||
|
### Error de llave
|
||
|
```python
|
||
|
mi_dicc = {'nombre': 'Alexanderson'}
|
||
|
mi_dicc['edad']
|
||
|
KeyError: 'edad'
|
||
|
```
|
||
|
|
||
|
### Levantar una excepción
|
||
|
```python
|
||
|
x = -5
|
||
|
if x < 0:
|
||
|
raise Exception('x debe ser positivo')
|
||
|
Exception: x debe ser positivo
|
||
|
```
|
||
|
|
||
|
### Assert
|
||
|
```python
|
||
|
x = -5
|
||
|
assert(x > 0), 'x no es positivo'
|
||
|
AssertionError: x no es positivo
|
||
|
```
|
||
|
|
||
|
## Manejo de excepciones
|
||
|
|
||
|
```python
|
||
|
try:
|
||
|
a = 5/0
|
||
|
except Exception as e:
|
||
|
print('ha ocurrido un error -->', e)
|
||
|
|
||
|
try:
|
||
|
a = 5/0
|
||
|
b = a + 'que'
|
||
|
except ZeroDivisionError as e:
|
||
|
print(e)
|
||
|
except TypeError as e:
|
||
|
print(e)
|
||
|
else:
|
||
|
print('todo esta bien')
|
||
|
finally:
|
||
|
print('siempre se llega aquí')
|
||
|
```
|
||
|
|
||
|
### Definir excepciones propias
|
||
|
|
||
|
Con clases de error propias. (SubClase de BaseException)
|
||
|
|
||
|
```python
|
||
|
class ValorMuyAltoError(Exception):
|
||
|
pass
|
||
|
|
||
|
class ValorMuyBajoError(Exception):
|
||
|
def __init__(self, message, value):
|
||
|
self.message = message
|
||
|
self.value = value
|
||
|
|
||
|
def test_valor(x):
|
||
|
if x > 100:
|
||
|
raise ValorMuyAltoError('El valor es muy alto')
|
||
|
if x < 5:
|
||
|
raise ValorMuyBajoError('El valor es muy bajo', x)
|
||
|
|
||
|
try:
|
||
|
test_valor(2)
|
||
|
except ValorMuyAltoError as e:
|
||
|
print(e)
|
||
|
except ValorMuyBajoError as e:
|
||
|
print(e.message, e.value)
|
||
|
```
|