python raise的用法

有关于python里raise显示引发异常的方法:

  • 当程序出错时,python会自动触发异常,也可以通过raise显示引发异常
  • 一旦执行了raise语句,raise之后的语句不再执行
  • 如果加入了try,except,那么except里的语句会被执行
try:
    s = None
    if s is None:
        print(' s 为空')
        raise NameError
    print(len(s))

except Exception:
    print('没有长度')
发布了41 篇原创文章 · 获赞 29 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43930694/article/details/104310929