• python学习——异常处理


     try:
    pass
    except 错误类型:
    针对这个错误要做的处理
    except 错误类型2:
    针对这个错误要做的处理
    except Exception as e:
    pass
    else:
    try中的代码没有问题时候执行
    finally:
    无论如何都执行
    异常处理结构1
     try:
    pass
    except 错误类型,错误类型2...:
    针对这些错误要做的处理
    except Exception as e:
    pass
    else:
    try中的代码没有问题时候执行
    finally:
    无论如何都执行

    搭配方法
    1.try...except
    2.try...except...else
    3.try...finally
    4.try...except...finally
    5.try...except...else...fianlly

    主动抛出异常:给其他开发者用的
     raise ValueError
     raise ValueError('你写的不对')


    断言——语法
     assert 1==2 # 只能接受一个布尔值 False 就抛异常
     assert 1==1 # 只能接受一个布尔值 True 就往下走
     print(1111)


    异常处理的忠告:在最外层的异常处理应该在所有的开发接受后才放
    try:
         main()
     except Exception as e:
      把错误写在文件里
  • 相关阅读:
    codeforces 1012C
    openjudge 6045:开餐馆
    openjudge 7624:山区建小学
    codevs 1040 统计单词个数
    openjudge9267:核电站
    openjudge7624:山区建小学
    bzoj3224:普通平衡树
    洛谷1137:旅行计划
    洛谷1095:守望者的逃离
    校内模拟赛:确定小组
  • 原文地址:https://www.cnblogs.com/bilx/p/11363479.html
Copyright © 2020-2023  润新知