文章目录
- 一.Python异常概述
- 1.1 处理ZeroDivisionError 异常
- 1.2 使用try-except 代码块
- 1.3 使用异常避免崩溃
- 1.4 处理FileNotFoundError 异常
- 1.5 分析文本
- 1.6 使用多个文件
- 1.7 失败时一声不吭
- 1.8 决定报告哪些错误
一.Python异常概述
Python使用被称为异常 的特殊对象来管理程序执行期间发生的错误。每当发生让Python不知所措的错误时,它都会创建一个异常对象。如果你编写了处理该异常的代码,程序将继续运行;如果你未对异常进行处理,程序将停止,并显示一个traceback,其中包含有关异常的报告。
异常是使用try-except 代码块处理的。try-except 代码块让Python执行指定的操作,同时告诉Python发生异常时怎么办。使用了try-except 代码块时,即便出现异常,程序也将继续运行:显示你编写的友好的错误消息,而不是令用户迷惑的traceback。
1.1 处理ZeroDivisionError 异常
下面来看一种导致Python引发异常的简单错误。你可能知道不能将一个数字除以0,但我们还是让Python这样做吧:
>>> print(5/0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
1.2 使用try-except 代码块
当你认为可能发生了错误时,可编写一个try-except 代码块来处理可能引发的异常。你让Python尝试运行一些代码,并告诉它如果这些代码引发了指定的异常,该怎么办。
处理ZeroDivisionError 异常的try-except 代码块类似于下面这样:
>>> try:
... print(5/0)
... except ZeroDivisionError:
... print("You can't divide by zero!")
...
You can't divide by zero!
1.3 使用异常避免崩溃
发生错误时,如果程序还有工作没有完成,妥善地处理错误就尤其重要。这种情况经常会出现在要求用户提供输入的程序中;如果程序能够妥善地处理无效输入,就能再提示用户提供有效输入,而不至于崩溃。
代码:
print("Give me two numbers, and I'll divide them.")
print("Enter 'q' to quit.")
while True:
first_number = input("\nFirst number:")
if first_number == 'q':
break
second_number = input("Second number: ")
if second_number == 'q':
break
try:
answer = int(first_number) / int(second_number)
except ZeroDivisionError:
print("You can't divide by 0!")
else:
print(answer)
测试记录:
E:\python\learn_python1\venv\Scripts\python.exe E:/python/learn_python1/exception_test1.py
Give me two numbers, and I'll divide them.
Enter 'q' to quit.
First number:10
Second number: 2
5.0
First number:0
Second number: 0
You can't divide by 0!
First number:q
Process finished with exit code 0
1.4 处理FileNotFoundError 异常
使用文件时,一种常见的问题是找不到文件:你要查找的文件可能在其他地方、文件名可能不正确或者这个文件根本就不存在。对于所有这些情形,都可使用try-except 代码块以直观的方式进行处理。
代码:
filename = 'abcdef.txt'
try:
with open(filename) as f_obj:
contents = f_obj.read()
except FileNotFoundError:
msg = "Sorry, the file " + filename + " does not exists."
print(msg)
测试记录:
E:\python\learn_python1\venv\Scripts\python.exe E:/python/learn_python1/except_test2.py
Sorry, the file abcdef.txt does not exists.
Process finished with exit code 0
1.5 分析文本
计算文本中有多少的单词
文本:
I love programming.
I love creating new games.
I also love finding meaning in large databases.
I lowe creating apps that can run in a browser.
代码:
filename = 'E:/python/file_test/programming.txt'
try:
with open(filename) as f_obj:
contents = f_obj.read()
except FileNotFoundError:
msg = "Sorry, the file " + filename + " does not exists."
print(msg)
else:
# 计算文件大致包含多少个单词
words = contents.split()
num_words = len(words)
print("The file " + filename + " has about " + str(num_words) + " words.")
测试记录:
E:\python\learn_python1\venv\Scripts\python.exe E:/python/learn_python1/except_test2.py
The file E:/python/file_test/programming.txt has about 26 words.
Process finished with exit code 0
1.6 使用多个文件
在某些场景,我们可能会有需求对多个文件进行操作。
文本:
db.txt
Oracle 1
MySQL 2
SQL Server 3
PostgreSQL 4
programming.txt
I love programming.
I love creating new games.
I also love finding meaning in large databases.
I lowe creating apps that can run in a browser.
代码:
def count_words(filename):
"""计算一个文件中大致包含多少个单词"""
try:
with open(filename) as f_obj:
contents = f_obj.read()
except FileNotFoundError:
msg = "Sorry, the file " + filename + "does not exist."
print(msg)
else:
# 计算文件大致包含多少个单词
words = contents.split()
num_words = len(words)
print("The file " + filename + " has about " + str(num_words) + " words.")
filenames = ['E:/python/file_test/programming.txt','E:/python/file_test/db.txt']
for filename in filenames:
count_words(filename)
测试记录:
E:\python\learn_python1\venv\Scripts\python.exe E:/python/learn_python1/except_test3.py
The file E:/python/file_test/programming.txt has about 26 words.
The file E:/python/file_test/db.txt has about 9 words.
Process finished with exit code 0
1.7 失败时一声不吭
在前一个示例中,我们告诉用户有一个文件找不到。但并非每次捕获到异常时都需要告诉用户,有时候你希望程序在发生异常时一声不吭,就像什么都没有发生一样继续运行。
要让程序在失败时一声不吭,可像通常那样编写try 代码块,但在except 代码块中明确地告诉Python什么都不要做。Python有一个pass 语句,可在代码块中使用它来让Python什么都不要做。
文本:
db.txt
Oracle 1
MySQL 2
SQL Server 3
PostgreSQL 4
– 不存在的文本
not_exist.txt
programming.txt
I love programming.
I love creating new games.
I also love finding meaning in large databases.
I lowe creating apps that can run in a browser.
代码:
def count_words(filename):
"""计算一个文件中大致包含多少个单词"""
try:
with open(filename) as f_obj:
contents = f_obj.read()
except FileNotFoundError:
pass
else:
# 计算文件大致包含多少个单词
words = contents.split()
num_words = len(words)
print("The file " + filename + " has about " + str(num_words) + " words.")
filenames = ['E:/python/file_test/programming.txt','E:/python/file_test/not_exist.txt','E:/python/file_test/db.txt']
for filename in filenames:
count_words(filename)
测试记录:
E:\python\learn_python1\venv\Scripts\python.exe E:/python/learn_python1/except_test3.py
The file E:/python/file_test/programming.txt has about 26 words.
The file E:/python/file_test/db.txt has about 9 words.
Process finished with exit code 0
1.8 决定报告哪些错误
在什么情况下该向用户报告错误?在什么情况下又应该在失败时一声不吭呢?如果用户知道要分析哪些文件,他们可能希望在有文件没有分析时出现一条消息,将其中的原因告诉他们。如果用户只想看到结果,而并不知道要分析哪些文件,可能就无需在有些文件不存在时告知他们。向用户显示他不想看到的信息可能会降低程序的可用性。Python的错误处理结构让你能够细致地控制与用户分享错误信息的程度,要分享多少信息由你决定。
编写得很好且经过详尽测试的代码不容易出现内部错误,如语法或逻辑错误,但只要程序依赖于外部因素,如用户输入、存在指定的文件、有网络链接,就有可能出现异常。凭借经验可判断该在程序的什么地方包含异常处理块,以及出现错误时该向用户提供多少相关的信息。
参考:
1.Python编程:从入门到实践