python中的assert是一种最简单的异常机制
assert的基本语法是:
"assert" expression1 ["," expression2]
expression1用于判断生成布尔值,当expression1为假的时候,则抛出异常,[]中的内容可选,即用户可以选择异常的提示值:
>>>a=23
>>>assert a==23
>>>a=a-1
>>>assert a==23
Traceback (most recent call last):
File "" , line 1, in
AssertionError
>>>assert a==23 , "error1"
Traceback (most recent call last):
File "" , line 1, in
AssertionError: error1