Python assert(断言)用于判断一个表达式,在表达式条件为 false 的时候触发异常。

即便 Python 程序的语法是正确的,在运行它的时候,也有可能发生错误。运行期检测到的错误被称为异常
try:
# age_str = input("====>")
# age = int(age_str)
# l1 = []
# l1[2]
d1 = {}
d1["name"]
except ValueError as e:
print(e)
except IndexError as e:
print(e)
except KeyError as e:
print("KeyError:", e)
except Exception as e:
print("其他未知异常")
# 异常的其他结构
s = "a"
s = 1
try:
int(s)
except ValueError as e:
print(e)
except KeyError as e:
print(e)
except IndexError as e:
print(e)
except Exception as e:
print("捕获未知异常")
else:
print("try 包含的代码块没有异常,执行else 里面的代码")
finally:
print("执行finally 的代码,通常用于释放资源")
s = "a"
try:
int(s)
except ValueError as e:
print(e)
finally:
print("执行finally 的代码,通常用于释放资源")
# 主动触发异常
try:
raise TypeError("这个异常是主动触发的")
except TypeError as e:
print(e)
自定义异常需要继承自 BaseException; (Exception 也 继承自 BaseException)
# 自定义异常
class MyException(BaseException):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return "<MyException:{}>".format(self.msg)
try:
raise MyException("自定义异常")
except MyException as e:
print(e)
程序某处判断结果,如果判断结果为 False; 抛出 AssertionError, 效果相当于 if 进行判断,再抛出异常
assert 1 == 2
if 1 != 2:
raise AssertionError免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理