From c36abae7c4d65d25e62bb70e339ee90f67e9349c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Fri, 4 Nov 2022 12:46:10 -0300 Subject: [PATCH] =?UTF-8?q?raise=20-=20lan=C3=A7ando=20exce=C3=A7=C3=B5es?= =?UTF-8?q?=20(erros)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aula95.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 aula95.py diff --git a/aula95.py b/aula95.py new file mode 100644 index 0000000..4a8fb37 --- /dev/null +++ b/aula95.py @@ -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'))