Python异常
概要: 抛出与处理Python中的错误和异常
创建时间: 2022.10.02 11:38:26
更新时间: 2022.10.02 11:42:08
错误与异常
如果我们把print
拼错为Print
,Python会抛出(Raise)一个语法错误。
示例程序块如下
运行结果如下
Text Only |
---|
| Traceback (most recent call last):
File "error.py", line 1, in <module>
Print("Hello World")
NameError: name 'Print' is not defined
|
在上述示例中,NameError
错误被抛出,同时Python检测错误发生的位置。这就是一个错误处理器(Error Handler)为我们的错误所做的事情
处理异常
我们可以通过使用try...except
来处理,一般来说,我们把普通的语句放在try
代码块中,将我们的错误处理器代码放在except
代码块中。
示例程序块如下
Python |
---|
| try:
text = input('Enter something -->')
except EOFError:
print('Why did you do an EOF on me?')
except KeyboardInterrupt:
print('You cancelled the operation.')
else:
print('You entered {}'.format(text))
|
运行结果如下
Text Only |
---|
| # Press ctrl + d
$ python exceptions_handle.py
Enter something --> Why did you do an EOF on me?
# Press ctrl + c
$ python exceptions_handle.py
Enter something --> ^CYou cancelled the operation.
$ python exceptions_handle.py
Enter something --> No exceptions
You entered No exceptions
|
在上述示例中,我们将所有可能引发异常或者错误的语句都放在了try
代码块中,并将相应的错误或者异常的处理器(Handler)放在except
子句或者代码块中。其中except
子句可以处理某种特定的错误或者异常,或者是一个在括号中列出的错误或者异常。如果没有提供错误或者异常的名称,那么它将处理所有的错误和异常。
提示
-
必须至少有一句except
字句与每一句try
字句相关联,否则try
代码块无意义
-
若没有任何错误或者异常被处理,那么将调用Python默认处理器,它会利用终端程序打印出错误信息
-
else
语句与try...except
代码块相关联,其作用是再没有发生异常的时候执行
抛出异常
在Python中,可以通过raise
语句来引发一次异常,具体的方法是提供错误名或者异常名以及要抛出(Thrown)异常的对象
提示
我们能够引发的错误或者异常必须是直接或者间接从属于Exception(异常)
类的派生类
示例程序块如下
Python |
---|
| class ShortInputException(Exception):
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
try:
text = input('Enter something -->')
if len(text) < 3:
raise ShortInputException(len(text), 3)
except EOFError:
print('Why did you do an EOF on me?')
except ShortInputException as ex:
print(('ShortInputException: The input wa ' + '{0} long, expected at least {1}').format(ex.length, ex.atleast))
else:
print('No exception was raised.')
|
运行结果如下
Text Only |
---|
| PS E:\sublime text 3 files\python3> python exceptions_raise.py
Enter something -->a
ShortInputException: The input wa 1 long, expected at least 3
PS E:\sublime text 3 files\python3> python exceptions_raise.py
Enter something -->abc
No exception was raised.
|
提示
-
在本例中,我们创建了名为ShortInputException
的异常类型。它包含两个字段:①获取给定输入文本长度的length
②程序期望的最小长度atleast
-
在except
子句中,我们提及了错误类,并将该类存储as
为相应的错误名或者异常名。这类似于函数调用中的形参和实参
-
在上述示例中,我们在except
子句中使用异常对象的length
和atleast
字段来向用户打印一条合适的信息
Try...Finally
在Python中,我们可以通过finally
代码块来确认文件对象是否被正确关闭
示例程序块如下
Python |
---|
| import sys
import time
f = None
try:
f = open("poem.txt")
while True:
line = f.readline()
if len(line) == 0:
break
print(line, end='')
sys.stdout.flush()
print("Press ctrl+c now")
time.sleep(2) # 运行间隔为2s
except IOError:
print("Could not find file poex.txt")
except KeyboardInterrupt:
print("!! You cancelled the reading from the file.")
finally:
if f:
f.close()
print("(Cleaning up: Closed the file)")
|
运行结果如下
Text Only |
---|
| PS E:\sublime text 3 files\python3> python exceptions_finally.py
Programing is fun
Press ctrl+c now
When the work is done
Press ctrl+c now
!! You cancelled the reading from the file.
(Cleaning up: Closed the file)
|
提示
-
在上例中,我们通过使用time.sleep
函数任意在每打印一行后插入两秒的休眠时间
-
在程序运行过程中,通过按下ctrl + c
来中断或者取消程序
-
在程序退出之前,finally
语句得到执行,文件对象被关闭
-
我们在print
之后使用了sys.stout.flush()
,以便它能被立即打印到屏幕上
with
语句
在上文中,我们在try
块中获取资源,然后再finally
块中释放资源是一种常见模式,此外,我们还有一个with
语句使得这一过程可以以另一种姿势来完成
示例程序块如下
Python |
---|
| with open("poem.txt") as f:
for line in f:
print(line, end='')
|
运行结果如下
Text Only |
---|
| Programing is fun
When the work is done
if you wanna make your work also fun:
use PYTHON!
|
提示
-
在此例中,我们使用open
函数与with
语句,而我们将关闭文件的操作交由with open
来完成
-
实际上,with
语句使用的协议会获取由open
语句返回的对象,再本案例中是thefile
-
程序再代码块开始之前调用thefile.__enter__
函数,并且总会在代码块执行完后调用thefile.__exit__
-
在使用finally
代码块中,应该格外留心__exit__
方法的自动操作,因为这样可以帮助我们便面重复显式使用try...finally
语句