raise - lançando exceções (erros)
This commit is contained in:
26
aula95.py
Normal file
26
aula95.py
Normal 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'))
|
||||
Reference in New Issue
Block a user