常见的异常种类

常见的异常

 
 
NameError  找不到这个名字 要么变量 要么函数
ValueError 在调用一个函数时给的的值不正确
TypeError 类型错误 例如字符串与数字加减乘除 调用一个不能被调用的类型
ZeroDivisionError 除数不能为0
KeyError 没有这个key
IndexError 索引不存在
StopIteration 没有更多的值可以迭代
FileNotFoundError 文件不存在
io.UnsupportedOperation 文件的操作不支持
AttributeError 没有这个属性
KeyboardInterrupt 程序被强行终止 ctrl+c
 
AttributeError 
class Foo:
    pass
f1 = Foo()
f1.x

FileNotFoundError

with open("xxxx.py","rt") as f:#文件不存在
    f.read()

ValueError

with open("异常.py","rt") as f:#文件不存在
    pass
f.read()

ZeroDivisionError
1/0

KeyError

{}["a"]

IndexError

l =[1,2,3]
l[4]

StopIteration

def shengchengqi():
    yield "a"
    yield "bb"

s1 = shengchengqi()
next(s1)
next(s1)
next(s1)#超出生成器的限界

主要常见的异常就这些。





猜你喜欢

转载自www.cnblogs.com/msj513/p/9889549.html