summaryrefslogtreecommitdiff
path: root/mit/exceptions.py
blob: 2208484357d6f7f6be0ceeb76ba04b139761ef68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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))