raise - lançando exceções (erros)

This commit is contained in:
Luiz Otávio
2022-11-04 12:46:10 -03:00
parent 9dd82e59af
commit c36abae7c4

26
aula95.py Normal file
View File

@@ -0,0 +1,26 @@
# raise - lançando exceções (erros)
# https://docs.python.org/pt-br/3/library/exceptions.html#built-in-exceptions
def nao_aceito_zero(d):
if d == 0:
raise ZeroDivisionError('Você está tentando dividir por zero')
return True
def deve_ser_int_ou_float(n):
tipo_n = type(n)
if not isinstance(n, (float, int)):
raise TypeError(
f'"{n}" deve ser int ou float. '
f'"{tipo_n.__name__}" enviado.'
)
return True
def divide(n, d):
deve_ser_int_ou_float(n)
deve_ser_int_ou_float(d)
nao_aceito_zero(d)
return n / d
print(divide(8, '0'))