美文网首页
python 12异常

python 12异常

作者: 6c0fe9142f09 | 来源:发表于2018-07-22 15:42 被阅读18次

常见异常处理

# 字典key空报错
dic = {}
print(dic["dashu"])# KeyError: 'dashu'

# 数组越界报错
lis = [1,2,3]
print(lis[4]) # IndexError: list index out of range

# 类型转换报错
s = "sd1"
print(int(s)) # ValueError: invalid literal for int() with base 10: 'sd1'

try-except-finally

# 当执行一个except后,不会执行之后的except
try:
    float("casdq")

    lis = [1,2]
    lis[3]

    dic = {}
    dic["123"]
except ValueError as e:
    print("ValueError",e.args)
except IndexError as e:
    print("IndexError",e.args)
except Exception as e:
    print("Exception",e.args)
finally :
    print("结束啦")

相关文章

  • python 12异常

    常见异常处理 try-except-finally

  • 24:python中的异常

    异常: 首先看看python的异常继承树 python的异常分为两种. 1、内建异常:就是python自己定义的异...

  • Python异常处理

    参考 Python菜鸟教程错误与异常 Python 异常处理 错误和异常 Python中(至少)有两种错误:语法错...

  • Python学习(八)

    异常处理 Python中的异常类型总结: Python内置异常类的层次结构: 异常检测 try-except语句 ...

  • (三)python错误与异常&面向对象编程

    python错误与异常 语法错误与定位 异常捕获、异常处理 try:xxxexcept: 自定义异常 python...

  • Python错误处理机制

    @(python程序员)[Python] Python Cookbook 捕获所有的异常 处理异常的时候最好还会尽...

  • 编程入门13:Python文本处理

    上一篇:编程入门12:Python异常处理 计算机经常需要对文本进行各种操作,我们知道Python语言表示文本的类...

  • Python异常处理

    Python中文件的操作 Python异常的处理 异常的引发 try⋯finally的使用

  • Python常见异常

    python标准异常 异常名称 描述 BaseException 所有异常的基...

  • Python 异常

    Python 含有异常处理机制来帮助用户处理可能发生的错误异常。 1. 异常概念 异常是指Python程序运行过程...

网友评论

      本文标题:python 12异常

      本文链接:https://www.haomeiwen.com/subject/pxglmftx.html