diff options
Diffstat (limited to 'mit/exceptions.py')
| -rw-r--r-- | mit/exceptions.py | 68 |
1 files changed, 68 insertions, 0 deletions
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)) |
