8.1 什么是异常
异常即程序运行时产生的错误
- Python使用异常对象来表示异常状态,并在遇到错误时引发异常。
- 异常可能会在程序执行过程中突然发生,打断正常代码流程,致使程序崩溃。
- 每个异常都是一个具体类的实例,可以通过try/except代码块捕获并处理异常,避免程序崩溃。
def zero_division():
a=1/0
return a
zero_division()
#运行结果
ZeroDivisionError: division by zero
8.2 引发异常
Python 中使用 raise 语句引发异常。
raise 语句需要将一个类(必须是Exception的子类)或实例作为参数。
将类作为参数时,将自动创建一个实例
raise Exception
#运行结果
Exception
raise Exception('Oops...Some thing went wrong.')
#运行结果
Exception: Oops...Some thing went wrong.
Python中一些常见内置异常类
8.3 自定义异常类
Python 中自定义异常类请务必直接或间接地继承Exception
class SomeCustomException(Exception):
pass
class MyCustomError(Exception):
def __init__(self):
super().__init__("Some custom error info.")
class CustomError(Exception):
def __init__(self,message):
self.message=message
super().__init__(self.message)
注:也可以继承自BaseException来创建自定义异常类,但通常不建议这么做
8.4 捕获异常
8.4.1 捕获异常
在程序中主动对异常进行处理,从而避免程序崩溃,称之为捕获异常
python 中使用try/except语句捕获异常
try:
x=int(input('Enter the first number:'))
y=int(input('Enter the second number:'))
print(x/y)
except ZeroDivisionError:
print("The second number cant't be zero!")
#运行结果
Enter the first number:1
Enter the second number:0
The second number cant't be zero!
8.4.2 捕获后重新引发异常
def calculate(x,y):
try:
result=x/y
except ZeroDivisionError:
raise
class MuffledCalculator:
muffled=False
def calc(self,expr):
try:
return eval(expr)
except ZeroDivisionError:
if self.muffled:
print('Division by zero is illegal')
else:
raise
8.4.3 捕获异常后引发别的异常
在这种情况下,导致进入 except 子句的异常将被作为异常上下文存储起来,并出现在最终的错误消息中:
try:
1/0
except ZeroDivisionError:
raise ValueError
#运行结果
ZeroDivisionError: division by zero
ValueError
可使用 raise … from … 语句来提供自己的异常上下文,也可使用None 来禁用上下文。
try:
1/0
except ZeroDivisionError:
raise ValueError from None
#运行结果
ValueError
8.4.4 多个except子句
try:
x=int(input('Enter the first number:'))
y=int(input('Enter the second number:'))
print(x/y)
except ZeroDivisionError:
print("The second number can't be zero!")
except TypeError:
print("That wasn't a number,was it?")
# 可使用一个 except 子句捕获多种异常,可在一个元组中指定这些异常
# (这种方式使你同时显式地捕获了异常对象本身)
try:
x=int(input('Enter the first number:'))
y=int(input('Enter the second number:'))
print(x/y)
except (ZeroDivisionError, ValueError) as e:
print(e)
8.4.5 捕获所有异常
# Don't do this.
try:
x=int(input('Enter the first number:'))
y=int(input('Enter the second number:'))
print(x/y)
except: BaseException
print('Something wrong happened ...')
# Don't do this.
try:
x=int(input('Enter the first number:'))
y=int(input('Enter the second number:'))
print(x/y)
except:
print('Something wrong happened ...')
8.5 else子句
有时在没有出现异常时执行一个代码块很有用,为此可以给try/except语句添加一个else子句,就像条件语句和循环一样
while True:
try:
x=int(input('Enter the first number:'))
y=int(input('Enter the second number:'))
value = x / y
print('x/y is', value)
except:
print('Invalid input. Please try again.')
else:
break
#运行结果
Enter the first number:1
Enter the second number:0
Invalid input. Please try again.
Enter the first number:2
Enter the second number:1
x/y is 2.0
8.6 finally子句
在发生异常时执行清理工作,不发生异常时 finally 子句的代码块会被执行吗?
x=None
try:
x=1/1
finally:
print('Cleaning up ...')
del x
#运行结果
Cleaning up ...
8.7 异常和函数
如果不处理函数中引发的异常,它将向上传播到调用函数的地方。如果在那里也未得到处理,异常将继续传播,直至到达主程序(全局作用域)。
如果主程序中也没有异常处理程序,程序将终止并显示栈跟踪消息。
def faulty():
raise Exception('Something is wrong!')
def ignore_exception():
faulty()
def handle_exception():
try:
faulty()
except:
print('Exception handled')
ignore_exception()
# handle_exception()
8.8 异常判断好于条件判断
如果你知道代码可能引发某种异常,且不希望出现这种异常时程序终止并显示栈跟踪消息,可添加必要的try/except或try/finally语句(或结合使用)来处理它。
有时也可使用条件判断来达成异常处理实现的目标,但在Python中更建议使用异常判断。
因为【请求宽恕比获得允许更容易】。直接去做,有问题再处理,而不是预先做大量的检查。
8.9 警告
可使用模块warnings中的函数warn发出一条警告,指出情况偏离了正轨。注意警告不是异常!
from warnings import warn
warn('Some warn.')
warn('Some warn.')
warn('Some warn.')
print('Hello World!')
如果其他代码在使用你的模块,其他可使用模块warnings中的函数filterwarnings来抑制你发出的警告(或特定类型的警告),并指定要采取的措施,如" error “或” ignore "。
from warnings import warn
from warnings import filterwarnings
filterwarnings("ignore")
warn("Anyone out there?")
filterwarnings("error")
warn("Something is very wrong!")
#运行结果
UserWarning: Something is very wrong!
引发的异常为UserWarning。发出警告时,可指定将引发的异常(即警告类别),但必须是Warning 的子类。