summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics/try2.py
blob: 11e60b3c227842a7e24c55f9bc845d3f02e8c27c (plain) (blame)
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
# nested try's

try:
    print("try 1")
    try:
        print("try 2")
        foo()
    except:
        print("except 2")
    bar()
except:
    print("except 1")

try:
    print("try 1")
    try:
        print("try 2")
        foo()
    except TypeError:
        print("except 2")
    bar()
except NameError:
    print("except 1")

# raised exception not contained in except tuple
try:
    try:
        raise Exception
    except (RuntimeError, SyntaxError):
        print('except 2')
except Exception:
    print('except 1')

# Check that exceptions across function boundaries work as expected
def func1():
    try:
        print("try func1")
        func2()
    except NameError:
        print("except func1")

def func2():
    try:
        print("try func2")
        foo()
    except TypeError:
        print("except func2")

func1()