python面向对象--异常处理

1.常见异常类型

IOError 文件读写异常
ValueError值异常,一般是数据类型不对应
IndexError下标索引越界

2.try...except...

try:
    f=open('test.txt')
except IOError as e:
    print(e)
>>:
[Errno 2] No such file or directory: 'test.txt'     

3.错误基类Exception

try:
    print(a)
except Exception as e:
    print(e)
>>:     
name 'a' is not defined     

猜你喜欢

转载自blog.51cto.com/13803166/2130055