/
exception.py
229 lines (187 loc) · 5.47 KB
/
exception.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#! /usr/bin/python3
#-*- coding:utf-8 -*-
"""
#文件名称:exception.py
#编写人员:LHH
#项 目 组:系统组
#创建日期:2020/07/08
#功能描述:异常
#修改描述:
#备注:
"""
repeat_num = 45
"""
异常:
1.异常的概念: 程序运行时停止执行并提示错误信息, 称之为抛出异常
2.捕获异常:
3.异常的传递:
4.自定义异常:
"""
print("****************1.简单的捕获异常*****************")
"""
开发中,如果对某些代码的执行不能确定是否正确, 用try()来捕获异常
简单的语法格式:
try:
执行的代码块
except:
异常处理
"""
def except_a():
is_ok = True
while True:
#简单的捕获异常并处理
try:
num = int(input('请输入数字: '))
is_ok = True
except:
print('您的输入有误,请重新输入数字!')
is_ok = False
if not is_ok:
continue
else:
break
except_a()
print("%s" % ('-' * (repeat_num - 15)))
print("****************2.根据错误类型捕获异常和未知错误*****************")
"""
针对不同类型的异常,做出不同的响应, 这个时候就需要捕获错误类型
语法:
try
pass
except 错误类型1:
pass
except (错误类型2, 错误类型3):
pass
except Exception as result:
print('未知错误 %s' % result)
"""
def except_b():
is_ok = True
while True:
#捕获错误类型异常
try:
num = int(input('请输入数字: '))
res = 10 / num
print('运算结果:%d' % res)
is_ok = True
# #输入数字以外的其他字符,出现的异常
# except ValueError:
# print("ValueError: invalid literal for int() with base 10: '%s'" % num)
# is_ok = False
#
# #输入0,除0异常
# except ZeroDivisionError:
# print('ZeroDivisionError: division by zero!')
# is_ok = False
#未知错误异常处理,可以是上面两个错误类型或其他任何错误
except Exception as rest:
print('未知错误:%s' % rest)
is_ok = False
if not is_ok:
continue
else:
break
except_b()
print("%s" % ('-' * (repeat_num - 15)))
print("****************3.捕获异常完整语法*****************")
"""
捕获异常的完整语法:
try:
pass
except 错误类型1:
pass
except 错误类型2:
pass
except (错误类型3, 错误类型4):
pass
except Exception as res:
#未知错误执行的代码
pass
else:
#没有任何异常时,才会执行的代码
pass
finally:
#无论是否有异常,都会执行的代码
pass
"""
def except_c():
is_ok = True
while True:
#捕获错误类型异常
try:
num = int(input('请输入数字: '))
res = 10 / num
print('运算结果:%d' % res)
is_ok = True
# #输入数字以外的其他字符,出现的异常
# except ValueError:
# print("ValueError: invalid literal for int() with base 10: '%s'" % num)
# is_ok = False
#
#输入0,除0异常
except ZeroDivisionError:
print('ZeroDivisionError: division by zero!')
is_ok = False
#未知错误异常处理,可以是上面两个错误类型或其他任何错误
except Exception as rest:
print('未知错误:%s' % rest)
is_ok = False
# 没有任何异常时,才会执行的代码
else:
print('没有任何异常')
# 无论是否有异常,都会执行的代码
finally:
print('任何情况都会执行')
if not is_ok:
continue
else:
break
except_c()
print("%s" % ('-' * (repeat_num - 15)))
print("****************4.异常的传递性*****************")
"""
异常的传递:当函数/方法出现异常时,会将异常传递给函数/方法的调用方,如果传递到主程序,仍然没有处理异常,程序才会被终止
技巧:为了保证代码的整洁性、且不需要大量使用异常捕获,只需在主函数中增加异常捕获,因为主函数调用了其他函数,只要其他函数出现异常,就会传递到主函数的捕获异常中
"""
def func1():
num = int(input('请输入数字:'))
return num
def func2():
return func1()
def func():
is_ok = True
while True:
try:
print(func2())
is_ok = True
except Exception as res:
print('未知错误:%s' % res)
is_ok = False
if not is_ok:
continue
else:
break
#调用
func()
print("%s" % ('-' * (repeat_num - 15)))
print("****************5.主动抛出异常*****************")
"""
主动抛出异常:
1.创建python提供的Exception异常类的对象
2.使用raise关键字抛出异常对象
开发中根据应用程序的业务需求,主动抛出异常
示例:提示用户输入密码,密码长度至少8位及以上,否则抛出异常
"""
def passwd():
pwd = input('请输入密码:')
if len(pwd) >= 8:
print('您输入的密码为:%s' % pwd)
else:
print('主动抛出异常:')
ext = Exception('密码长度至少8位')
raise ext
try:
passwd()
except Exception as res:
print(res)
print("%s\n" % ('-' * repeat_num))