Skip to content

Commit 9ed0d44

Browse files
committed
Add Code
1 parent 16b068e commit 9ed0d44

File tree

10 files changed

+92
-0
lines changed

10 files changed

+92
-0
lines changed

Chapter 7/7_1_1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 1.最简单的,try捕获了任何异常,直接丢给except后的代码块处理:
2+
try:
3+
result = 1 / 0
4+
except:
5+
print("捕获到异常了!")

Chapter 7/7_1_10.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
assert 断言使用示例
3+
"""
4+
a, b = 1, 2
5+
assert a > b
6+
assert (a > b), ("{a} 并不比 {b} 大'".format(a = a, b = b))

Chapter 7/7_1_2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 2.捕获特定类型:
2+
try:
3+
result = 1 / 0
4+
except ZeroDivisionError:
5+
print("捕获到除数为零的错误")

Chapter 7/7_1_3.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 3.针对不同的异常设置多个except
2+
try:
3+
sum = 1 + '2'
4+
result = 1 / 0
5+
except TypeError as reason:
6+
print("类型出错:" + str(reason))
7+
except ZeroDivisionError as reason:
8+
print("除数为0:" + str(reason))

Chapter 7/7_1_4.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 4.对多个异常统一处理
2+
try:
3+
result = 1 / 0
4+
sum = 1 + '2'
5+
except (TypeError, ZeroDivisionError) as reason:
6+
print(str(reason))

Chapter 7/7_1_5.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 5.当没有检测到异常时才执行的代码块,可以用else
2+
try:
3+
result = 4 / 2
4+
except ZeroDivisionError as reason:
5+
print(str(reason))
6+
else:
7+
print("没有发生异常,输出结果:%d" % result)

Chapter 7/7_1_6.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 6.无论是否发生异常都会执行的一段代码块,比如io流关闭,
2+
# 可以使用finally子句,如果发生异常先走except子句,后走finally子句。
3+
try:
4+
result = 4 / 2
5+
except ZeroDivisionError as reason:
6+
print(str(reason))
7+
else:
8+
print("没有发生异常,输出结果:%d" % result)
9+
finally:
10+
print("无论是否发生异常都会执行~")

Chapter 7/7_1_7.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
raise显示抛出一个异常示例
3+
"""
4+
import re
5+
6+
# 注:这段正则你只需要知道是用来匹配电话号码的,正则在爬虫那一部分会详细讲解
7+
phone_compile =re.compile(r'^(0|86|17951)?(13[0-9]|14[579]|15[0-35-9]|17[01678]|18[0-9])[0-9]{8}$')
8+
number = input("请输入一串手机号码:")
9+
if phone_compile.match(number) is not None:
10+
print("您输入的手机号码是:%s" % number)
11+
else:
12+
raise ValueError

Chapter 7/7_1_8.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
自定义电话号码异常示例
3+
"""
4+
import re
5+
6+
# 自定义异常
7+
class PhoneNumberException(Exception):
8+
def __init__(self, message):
9+
Exception.__init__(self)
10+
self.message = message
11+
12+
13+
if __name__ == '__main__':
14+
phone_compile = re.compile(r'^(0|86|17951)?(13[0-9]|14[579]|15[0-35-9]|17[01678]|18[0-9])[0-9]{8}$')
15+
number = input("请输入一串手机号码:")
16+
if phone_compile.match(number) is not None:
17+
print("您输入的手机号码是:%s" % number)
18+
else:
19+
raise PhoneNumberException("非法的手机号码!")

Chapter 7/7_1_9.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
sys.exc_info函数获得异常信息示例
3+
"""
4+
5+
import sys
6+
7+
try:
8+
result = 1 / 0
9+
except:
10+
tuple_exception = sys.exc_info()
11+
12+
# 输出结果依次是:异常类,类示例,跟踪记录对象
13+
for i in tuple_exception:
14+
print(i)

0 commit comments

Comments
 (0)