-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathexceptions_test.py
More file actions
173 lines (141 loc) · 2.84 KB
/
exceptions_test.py
File metadata and controls
173 lines (141 loc) · 2.84 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#Empty Except
def ee1(val):
try:
val.attr
except:
pass
def ee1(val):
try:
val.attr()
except TypeError:
pass
def ee2(val):
try:
val.attr
except Error:
#But it is OK if there is a comment
pass
#OK with an else clause as well...
def ee3(val):
try:
val.attr
except Error:
pass
else:
return 42
class NotException1(object):
pass
class NotException2(object):
pass
def illegal_raise_type():
raise NotException1
def illegal_raise_value1():
raise "Exception"
def illegal_raise_value2():
raise NotException2()
def illegal_handler():
try:
illegal_raise()
except NotException1:
#Must do something
print("NotException1")
except NotException2:
#Must do something
print("NotException2")
#Incorrect except order
try:
val.attr
except Exception:
print (2)
except AttributeError:
print (3)
#Catch BaseException
def catch_base_exception():
try:
illegal_raise()
except BaseException:
#Consumes KeyboardInterrupt
pass
def catch_base_exception_ok():
try:
illegal_raise()
except BaseException:
raise
def legal_handler1():
try:
illegal_raise()
except (IOError, KeyError):
print ("Caught exception")
pair = IOError, KeyError
triple = pair, AttributeError
def legal_handler2():
try:
illegal_raise()
except pair:
print ("Caught exception")
try:
illegal_raise()
except triple:
print ("Caught exception")
def legal_handler3():
try:
illegal_raise()
except could_be_anything():
print ("Caught exception")
def a_number():
return 4.0
def illegal_handler2():
try:
illegal_raise()
except a_number():
print ("Caught exception")
def stop_iter_ok(seq):
try:
next(seq)
except StopIteration:
pass
#Guarded None in nested function
def f(x=None):
def inner(arg):
if x:
raise x
#ODASA-4705
def g(cond):
try:
if cond:
return may_raise_io_error()
else:
raise KeyError
except IOError:
pass # This is OK, as it is just passing to the following statement which handles the exception.
return 0
def ee4(x):
try:
del x.attr
except AttributeError:
pass
def ee5(x):
try:
x[0]
except IndexError:
pass
def ee6(x):
try:
del x[0]
except IndexError:
pass
def ee7(x):
try:
delattr(x, "attr")
except AttributeError:
pass
def ee8(x):
try:
x.encode("martian-18")
except UnicodeEncodeError:
pass
#These are so common, we give warnings not errors.
def foo():
raise NotImplemented
def bar():
raise NotImplemented()