From d98f46ce647846b0aa30b2e16a30fd4e152a1bf5 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 10 Jul 2025 22:55:07 +0200 Subject: Add new code Signed-off-by: Carlos Maiolino --- mit/exceptions.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 mit/exceptions.py (limited to 'mit/exceptions.py') diff --git a/mit/exceptions.py b/mit/exceptions.py new file mode 100644 index 0000000..2208484 --- /dev/null +++ b/mit/exceptions.py @@ -0,0 +1,68 @@ +#!/usr/bin/python3 + +def my_file(): + fname = input("File: ") + + try: + fh = open(fname, 'r') + except IOError: + print("Could not open file " + fname) + else: + print("Open: Success! File: " + fname) + + return fh + +def convert(): + var = input("Type in: ") + + try: + var = int(var) + except ValueError: # We can use exceptions to handle special cases + print("Converting to string") + var = str(var) + + return var + +def convert_file(): + fhandle = my_file() + + my_list = [] + + for line in fhandle: + e = line.split(',') + + for i in e: + try: + print("Trying as int") + my_list.append(int(i)) + except ValueError: + print("Opsi, element is not a number, converting to string") + my_list.append(str(i)) + + print(my_list) + return my_list + +def exe(): + try: + try: + raise Exception("0") + finally: + print("Foo") + except Exception as ex: + print(ex) + +def weird(): + try: + raise TypeError("Deu merda") + except TypeError as foo: + print(foo) + except: + print("Exception") + +def simple(): + try: + 'a' / 3 + except TypeError as err: + print("Opsy, the error was: " + str(err)) + + print(type(err)) -- cgit v1.2.3