#!/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))