1. 程式人生 > >python面向對象--異常處理

python面向對象--異常處理

UC pri open pen exce 下標索引 處理 dex fine

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     

python面向對象--異常處理