8.1 异常是什么
Python使用异常对象来表示异常状态,并在遇到错误时引发异常。异常对象未被处理(或捕 获)时,程序将终止并显示一条错误消息(traceback)。
1/0
# Traceback (most recent call last):
# File "<stdin>", line 1, in ?
# ZeroDivisionError: integer division or modulo by zero
8.2 让事情沿你指定的轨道出错
出现问题时,将自动引发异常。
8.2.1 raise语句
要引发异常,可使用raise语句。并将一个类(必须是Exception的子类)或实例作为参数。 将类作为参数时,将自动创建一个实例。
raise Exception
# Traceback (most recent call last):
# File "<stdin>", line 1, in ?
# Exception
raise Exception('hyperdrive overload')
# Traceback (most recent call last):
# File "<stdin>", line 1, in ?
# Exception: hyperdrive overload
常见内部异常类
- Exception 几乎所有的异常类都是从它派生而来的
- AttributeError 引用属性或给它赋值失败时引发
- OsError 操作系统不能执行指定任务时引发
- IndexError 使用序列中不存在的索引时引发 为LookupError的子类
- KeyError 使用映射中不存在的键时引发,为LookupError的子类
- NameError 找不到名称(变量)时引发
- SyntaxError 代码不正确时引发
- TypeError 类型不正确
8.2.2 自定义的异常类
那么如何创建异常类呢?就像创建其他类一样,但务必直接或间接地继承Exception(这意 味着从任何内置异常类派生都可以)。
class SomeCustomException(Exception):
pass
8.3 捕获异常
捕获异常的意思是对异常进行处理,使用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 can't be zero!")
8.3.2 多个except子句
为同时捕获异常,可在try/except语句中再添加一个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?")
8.3.3 一箭双雕
如果要使用一个except子句捕获多种异常,可在一个元组中指定这些异常
try:
x = int(input('Enter the first number: '))
y = int(input('Enter the second number: '))
print(x / y)
except (ZeroDivisionError, TypeError, NameError):
print('Your numbers were bogus ...')
8.3.4 捕获对象
try:
x = int(input('Enter the first number: '))
y = int(input('Enter the second number: '))
print(x / y)
except (ZeroDivisionError, TypeError, NameError) as e:
print('Your numbers were bogus ...')
8.3.5 一网打尽
如果你就是要使用一段代码捕获所有的异常,只需在except语句中不指定任何异常类即可。
try:
x = int(input('Enter the first number: '))
y = int(input('Enter the second number: '))
print(x / y)
except :
print('Something wrong happened ...')
8.3.6 万事大吉时
在有些情况下,在没有出现异常时执行一个代码块很有用。为此,可像条件语句和循环一样, 给try/except语句添加一个else子句。
try:
print('A simple task')
except:
print('What? Something went wrong?')
else:
print('Ah ... It went as planned.')
8.3.7 最后
最后,还有finally子句,可用于在发生异常时执行清理工作。这个子句是与try子句配套的。
x = None
try:
x = 1/0
finally:
print('Cleaning up ...')
del x
8.4 异常和函数
如果不处理函数中引发的异常,它将向上传播到调用函数的地 方。如果在那里也未得到处理,异常将继续传播,直至到达主程序(全局作用域)。如果主程序 中也没有异常处理程序,程序将终止并显示栈跟踪消息。
8.5 异常之禅
假设有一个字典,你要在指定的键存在时打印与之相关联的值,否则什么都不做。
可以用if语句实现
def describe_person(person):
print('Description of', person['name'])
print('Age:', person['age'])
if 'occupation' in person:
print('Occupation:', person['occupation'])
这段代码很直观,但效率不高(虽然这里的重点是代码简洁),因为它必须两次查找 'occupation’键:一次检查这个键是否存在(在条件中),另一次获取这个键关联的值,以便将其打印出来。
下面是另一种解决方案:
def describe_person(person):
print('Description of', person['name'])
print('Age:', person['age'])
try:
print('Occupation:', person['occupation'])
except KeyError:
pass
在这里,函数直接假设存在’occupation’键。如果这种假设正确,就能省点事:直接获取并 打印值,而无需检查这个键是否存在。如果这个键不存在,将引发KeyError异常,而except子句 将捕获这个异常。
8.6 不那么异常的情况
如果你只想发出警告,指出情况偏离了正轨,可使用模块warnings中的函数warn。
from warnings import warn
warn("I've got a bad feeling about this.")
警告只显示一次。如果再次运行最后一行代码,什么事情都不会发生。
如果其他代码在使用你的模块,可使用模块warnings中的函数filterwarnings来抑制你发出 的警告(或特定类型的警告),并指定要采取的措施,如"error"或"ignore"。