Python異常及處理

目錄

Python內置異常類的層次結構

Python異常處理


Python內置異常類的層次結構

BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
      +-- StopIteration
      +-- ArithmeticError
      |    +-- FloatingPointError
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      +-- AssertionError
      +-- AttributeError
      +-- BufferError
      +-- EOFError
      +-- ImportError
      +-- LookupError
      |    +-- IndexError
      |    +-- KeyError
      +-- MemoryError
      +-- NameError
      |    +-- UnboundLocalError
      +-- OSError
      |    +-- BlockingIOError
      |    +-- ChildProcessError
      |    +-- ConnectionError
      |    |    +-- BrokenPipeError
      |    |    +-- ConnectionAbortedError
      |    |    +-- ConnectionRefusedError
      |    |    +-- ConnectionResetError
      |    +-- FileExistsError
      |    +-- FileNotFoundError
      |    +-- InterruptedError
      |    +-- IsADirectoryError
      |    +-- NotADirectoryError
      |    +-- PermissionError
      |    +-- ProcessLookupError
      |    +-- TimeoutError
      +-- ReferenceError
      +-- RuntimeError
      |    +-- NotImplementedError
      +-- SyntaxError
      |    +-- IndentationError
      |         +-- TabError
      +-- SystemError
      +-- TypeError
      +-- ValueError
      |    +-- UnicodeError
      |         +-- UnicodeDecodeError
      |         +-- UnicodeEncodeError
      |         +-- UnicodeTranslateError
      +-- Warning
           +-- DeprecationWarning
           +-- PendingDeprecationWarning
           +-- RuntimeWarning
           +-- SyntaxWarning
           +-- UserWarning
           +-- FutureWarning
           +-- ImportWarning
           +-- UnicodeWarning
           +-- BytesWarning
           +-- ResourceWarning

AssertionError

斷言語句(assert)失敗。assert 語句後條件爲假時,程序自動崩潰並拋出 AssertionError 的異常。
一般用它在程序中置入檢查點,當需要確保程序中的某個條件一定爲真才能讓程序正常工作時,assert 關鍵字就非常有用。

>>> assert 3 < 4
>>> assert 4 < 3
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    assert 4 < 3
AssertionError
>>> my_list = ['ABC']
>>> assert len(my_list) > 0
>>> my_list.pop()
'ABC'
>>> assert len(my_list) > 0
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    assert len(my_list) > 0
AssertionError

AttributeError

嘗試訪問未知的對象屬性。當試圖訪問的對象屬性不存在時拋出的異常。

>>> my_list = []
>>> my_list.huhan
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    my_list.huhan
AttributeError: 'list' object has no attribute 'huhan'

EOFError

用戶輸入文件末尾標誌EOF(Ctrl+d)

FloatingPointError

浮點計算錯誤

GeneratorExit

generator.close()方法被調用的時候

ImportError

導入模塊失敗的時候

IndentationError

縮進錯誤

IndexError

索引超出序列的範圍

>>> my_list = [1, 2, 3]
>>> my_list[3]
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    my_list[3]
IndexError: list index out of range

KeyError

字典中查找一個不存在的關鍵字

>>> my_dict = {'one': 'いち', 'two': 'に', 'three': 'さん'}
>>> my_dict['one']
'いち'
>>> my_dict['four']
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    my_dict['four']
KeyError: 'four'

KeyboardInterrupt

用戶輸入中斷鍵(Ctrl+c)

MemoryError

內存溢出(可通過刪除對象釋放內存)

NameError

嘗試訪問一個不存在的變量

>>> abc
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    abc
NameError: name 'abc' is not defined

NotImplementedError

尚未實現的方法

OSError

操作系統產生的異常(例如打開一個不存在的文件)

>>> my_file = open('D:\\Python\\test\\no.txt', 'r')
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    my_file = open('D:\\Python\\test\\no.txt', 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\Python\\test\\no.txt'

OverflowError

數值運算超出最大限制

ReferenceError

弱引用(weak reference)試圖訪問一個已經被垃圾回收機制回收了的對象

RuntimeError

一般的運行時錯誤

StopIteration

迭代器沒有更多的值

SyntaxError

Python的語法錯誤

>>> print('a')
a
>>> print 'a'
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('a')?

TabError

Tab和空格混合使用

SystemError

Python編譯器系統錯誤

SystemExit

Python編譯器進程被關閉

TypeError

不同類型間的無效操作

>>> age = 12
>>> print("I'm " + str(age) +" years old.")
I'm 12 years old.
>>> 
>>> print("I'm " + age +" years old.")
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    print("I'm " + age +" years old.")
TypeError: can only concatenate str (not "int") to str

UnboundLocalError

訪問一個未初始化的本地變量(NameError的子類)

UnicodeError

Unicode相關的錯誤(ValueError的子類)

UnicodeEncodeError

Unicode編碼時的錯誤(UnicodeError的子類)

UnicodeDecodeError

Unicode解碼時的錯誤(UnicodeError的子類)

UnicodeTranslateError

Unicode轉換時的錯誤(UnicodeError的子類)

ValueError

傳入無效的參數

ZeroDivisionError

除數爲零

>>> 5 / 0
Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    5 / 0
ZeroDivisionError: division by zero

Python異常處理

try 語句

try-except 語句

語句格式:

try:
    檢測範圍
except Exception[as reason]:
    出現異常(Exception)後的處理代碼

try 語句檢測範圍內一旦出現異常,剩下的語句將不會被執行。

例:

f = open('不一定存在的文件.txt')
print(f.read())
f.close()

# 文件不存在時報錯:
'''
FileNotFoundError: [Errno 2] No such file or directory: '不一定存在的文件.txt'
'''
# 不帶參數,知其然不知其所以然~~
try:
    f = open('不一定存在的文件.txt')
    print(f.read())
    f.close()
except OSError:    # FileNotFoundError 屬 OSError
    print('文件或路徑不存在呀不存在~~~')

'''
文件或路徑不存在呀不存在~~~
'''
# 帶參數,列出錯誤原因。知其然亦知其所以然~~
try:
    f = open('不一定存在的文件.txt')
    print(f.read())
    f.close()
except OSError as reasonXYZ123_custom:  # 可以自定義
    print('文件或路徑不存在呀不存在~~~\n錯誤的原因是:' + str(reasonXYZ123_custom))  # 需將錯誤內容強制變爲字符串

'''
文件或路徑不存在呀不存在~~~
錯誤的原因是:[Errno 2] No such file or directory: '不一定存在的文件.txt'
'''

一個try語句還可以搭配多個except語句,對關注的異常進行處理。

try:
    sum = 1 + '1'  # 發現異常將跳過其他語句
    f = open('不一定存在的文件.txt')
    print(f.read())
    f.close()

except OSError as reasonXYZ123:
    print('文件或路徑不存在呀不存在~~~\n錯誤的原因是:' + str(reasonXYZ123))
except TypeError as reasonXYZ123:  #這個錯誤先發生~
    print('類型出錯啦~~~\n錯誤的原因是:' + str(reasonXYZ123))

'''
類型出錯啦~~~
錯誤的原因是:unsupported operand type(s) for +: 'int' and 'str'
'''

對異常進行統一處理:
except後邊還可以跟多個異常,然後對這些異常進行統一的處理:

try:
    sum = 1 + '1'  # 錯上加錯~
    f = open('不一定存在的文件.txt')
    print(f.read())
    f.close()

except (TypeError, OSError) as reasonXYZ123:  # except後邊跟多個異常
    print('出錯啦~~~\n錯誤的原因是:' + str(reasonXYZ123))

'''
出錯啦~~~
錯誤的原因是:unsupported operand type(s) for +: 'int' and 'str'
'''
try:
    sum = 1 + '1'
    f = open('不一定存在的文件.txt')
    print(f.read())
    f.close()

except (TypeError, OSError):  # 不報Error原因
    print('出錯啦~~~')

'''
出錯啦~~~
'''

捕獲所有異常:
缺點:會隱藏所有未想到並且未做好處理準備的錯誤。如:Ctrl+C 試圖終止程序,卻被解釋爲 KeyboardInterrupt 異常。

try:
    int('abc')  # 錯1
    sum = 1 + '1'  # 錯2
    f = open('不一定存在的文件.txt')  # 錯3
    print(f.read())
    f.close()

except:
    print('出錯啦!!!')

'''
出錯啦!!!
'''

try-finally 語句

定義清理行爲。如果一個異常在 try 子句裏(或者在 except 和 else 子句裏)被拋出,而又沒有任何的 except 把它截住,那麼這個異常會在 finally 子句執行後再次被拋出。

finally 語句塊無論如何都將被執行:
try 語句塊沒有錯誤:跳過 except 語句塊執行 finally 語句塊的內容。
try 語句塊出現錯誤:先執行 except 語句塊再執行 finally 語句塊的內容。

try:
    檢測範圍
except Exception[as reason]:
    出現異常(Exception)後的處理代碼
finally:
    無論如何都會被執行的代碼

例:

try:
    f = open('D:\\Python\\test\\存在的文件.txt', 'w')  # 成功打開
    print(f.write('寫入文件的東西'))  # 寫入字符串,返回字符長度
    sum = 1 + '1'  # Error
    f.close()  # 因爲Error,跳過關閉操作,數據在緩存,寫入未保存至文件

except (TypeError, OSError):
    print('出錯啦~~~')

'''
7
出錯啦~~~
'''

解決上例未保存的問題: 

try:
    f = open('D:\\Python\\test\\存在的文件.txt', 'w')
    print(f.write('寫入文件的東西'))
    sum = 1 + '1'  # Error

except (TypeError, OSError):
    print('出錯啦~~~')

finally:
    f.close()  # 無論如何都將被執行

'''
7
出錯啦~~~
'''

raise 語句

使用 raise 語句拋出一個指定的異常。

>>> raise  # 單獨使用
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    raise
RuntimeError: No active exception to reraise
>>> 
>>> raise ZeroDivisionError  # 拋出指定異常
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    raise ZeroDivisionError
ZeroDivisionError
>>> 
>>> raise ZeroDivisionError('除數不能爲零~~!')  # 加參數,對異常進行解釋
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    raise ZeroDivisionError('除數不能爲零~~!')
ZeroDivisionError: 除數不能爲零~~!

else 語句

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章