summaryrefslogtreecommitdiff
path: root/tests/functional/t/try_except_raise.py
blob: b82a4bba150adf807e036d937e9eec0eb58e0dc8 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# pylint:disable=missing-docstring, unreachable, bad-except-order, bare-except, unnecessary-pass
# pylint: disable=undefined-variable, broad-except, raise-missing-from, too-few-public-methods
try:
    int("9a")
except:  # [try-except-raise]
    raise

try:
    int("9a")
except:
    raise ValueError('Invalid integer')


try:
    int("9a")
except:  # [try-except-raise]
    raise
    print('caught exception')

try:
    int("9a")
except:
    print('caught exception')
    raise


class AAAException(Exception):
    """AAAException"""
    pass

class BBBException(AAAException):
    """BBBException"""
    pass

def ccc():
    """try-except-raise test function"""

    try:
        raise BBBException("asdf")
    except BBBException:
        raise
    except AAAException:
        raise BBBException("raised from AAAException")


def ddd():
    """try-except-raise test function"""

    try:
        raise BBBException("asdf")
    except AAAException:
        raise BBBException("raised from AAAException")
    except:  # [try-except-raise]
        raise

try:
    pass
except RuntimeError:
    raise
except:
    print("a failure")

try:
    pass
except:
    print("a failure")
except RuntimeError:  # [try-except-raise]
    raise

try:
    pass
except:  # [try-except-raise]
    raise
except RuntimeError:
    print("a failure")

try:
    pass
except (FileNotFoundError, PermissionError):
    raise
except OSError:
    print("a failure")

class NameSpace:
    error1 = FileNotFoundError
    error2 = PermissionError
    parent_error=OSError

try:
    pass
except (NameSpace.error1, NameSpace.error2):
    raise
except NameSpace.parent_error:
    print("a failure")

# also consider tuples for subsequent exception handler instead of just bare except handler
try:
    pass
except (FileNotFoundError, PermissionError):
    raise
except (OverflowError, OSError):
    print("a failure")

try:
    pass
except (FileNotFoundError, PermissionError):  # [try-except-raise]
    raise
except (OverflowError, ZeroDivisionError):
    print("a failure")

try:
    pass
except (FileNotFoundError, PermissionError):
    raise
except Exception:
    print("a failure")

try:
    pass
except (FileNotFoundError, PermissionError):
    raise
except (Exception,):
    print("a failure")