diff --git a/README.md b/README.md new file mode 100644 index 0000000..b057bc6 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ + Python 3 +Test exceptions diff --git a/classexeption.py b/classexeption.py new file mode 100644 index 0000000..69140af --- /dev/null +++ b/classexeption.py @@ -0,0 +1,10 @@ +class MiError(Exception): + def __init__(self, valor): + self.valor = valor + def __str__(self): + return repr(self.valor) + +try: + raise MiError(2*2) +except MyError as e: + print('Ocurrió mi excepción, valor:', e.encode('utf-8')) diff --git a/classexeption2.py b/classexeption2.py new file mode 100644 index 0000000..5f440f4 --- /dev/null +++ b/classexeption2.py @@ -0,0 +1,35 @@ +# define Python user-defined exceptions +class Error(Exception): + """Base class for other exceptions""" + pass + +class ValueTooSmallError(Error): + """Raised when the input value is too small""" + pass + +class ValueTooLargeError(Error): + """Raised when the input value is too large""" + pass + +# our main program +# user guesses a number until he/she gets it right + +# you need to guess this number +number = 10 + +while True: + try: + i_num = int(input("Enter a number: ")) + if i_num < number: + raise ValueTooSmallError + elif i_num > number: + raise ValueTooLargeError + break + except ValueTooSmallError: + print("This value is too small, try again!") + print(" ") + except ValueTooLargeError: + print("This value is too large, try again!") + print(" ") + +print("Congratulations! You guessed it correctly.") diff --git a/denominator.py b/denominator.py new file mode 100644 index 0000000..62e3de7 --- /dev/null +++ b/denominator.py @@ -0,0 +1,11 @@ +while True: + # Read int from console. + denominator = int(input()) + + # Use int as denominator. + try: + i = 1 / denominator + except: + print("Error") + else: + print("OK") diff --git a/divzero.py b/divzero.py new file mode 100644 index 0000000..cfb0c62 --- /dev/null +++ b/divzero.py @@ -0,0 +1,10 @@ +def mistake(name): + raise Exception(name+" caused exception") + + +try: + x=1/0 +except ZeroDivisionError: + print ("Tryed div by zero") +print ("salir") +mistake("error de conexion") diff --git a/openfile.py b/openfile.py new file mode 100644 index 0000000..b9d0570 --- /dev/null +++ b/openfile.py @@ -0,0 +1,5 @@ +try: + f=open("abc") +except: + print("except hit") + raise diff --git a/raise.py b/raise.py new file mode 100644 index 0000000..1ad930d --- /dev/null +++ b/raise.py @@ -0,0 +1 @@ +raise ZeroDivisionError