抛出异常
raise
raise [Exception [, args [, traceback]]]# 如果 x 大于 5 就触发异常
x = 10
if x > 5:
raise Exception('x 不能大于 5。x 的值为: {}'.format(x))>>> try:
raise NameError('HiThere')
except NameError:
print('An exception flew by!')
raise
An exception flew by!
Traceback (most recent call last):
File "<stdin>", line 2, in ?
NameError: HiThereassert
最后更新于