1. 程式人生 > 其它 >【python基礎】python異常處理

【python基礎】python異常處理

一、異常的種類
在python中不同的異常可以用不同的型別去標識,一個異常標識一種錯誤。

1 、常用異常類
AttributeError 試圖訪問一個物件沒有的樹形,比如foo.x,但是foo沒有屬性x
IOError 輸入/輸出異常;基本上是無法開啟檔案
ImportError 無法引入模組或包;基本上是路徑問題或名稱錯誤
IndentationError 語法錯誤(的子類) ;程式碼沒有正確對齊
IndexError 下標索引超出序列邊界,比如當x只有三個元素,卻試圖訪問x[5]
KeyError 試圖訪問字典裡不存在的鍵
KeyboardInterrupt Ctrl+C被按下
NameError 使用一個還未被賦予物件的變數
SyntaxError Python程式碼非法,程式碼不能編譯(個人認為這是語法錯誤,寫錯了)
TypeError 傳入物件型別與要求的不符合
UnboundLocalError 試圖訪問一個還未被設定的區域性變數,基本上是由於另有一個同名的全域性變數,導致你以為正在訪問它
ValueError 傳入一個呼叫者不期望的值,即使值的型別是正確的
2、異常舉例:

# TypeError:int型別不可迭代
for i in 3:
    pass

# ValueError
num=input(">>: ") #輸入hello
int(num)

# NameError
aaa

# IndexError
l=['egon','aa']
l[3]

# KeyError
dic={'name':'egon'}
dic['age']

# AttributeError
class Foo:pass
Foo.x

# ZeroDivisionError:無法完成計算
res1=1/0
res2=1+'str'

二、異常處理
1、基本語法try...except

try:
    被檢測的程式碼塊
except 異常型別:
    try中一旦檢測到異常,就執行這個位置的邏輯

舉例

try:
    f = [ 'a', 'a', 'a','a','a', 'a','a',]
    g = (line.strip() for line in f) #元組推導式
    print(next(g))
    print(next(g))
    print(next(g))
    print(next(g))
    print(next(g))
except StopIteration:
    f.close()

異常類只能用來處理指定的異常情況,如果非指定異常則無法處理。

s1 = 'hello'
try:
    int(s1)
except IndexError as e:  # 未捕獲到異常,程式直接報錯
    print(e)

2、多分支異常 except..except與萬能異常:Exception

s1 = 'hello'
try:
    int(s1)
except IndexError as e:
    print(e)
except KeyError as e:
    print(e)
except ValueError as e:
    print(e)
except Exception as e:
    print(e)

3、try/except...else
try/except 語句還有一個可選的 else 子句,如果使用這個子句,那麼必須放在所有的 except 子句之後。

else 子句將在 try 子句沒有發生任何異常的時候執行。

for arg in sys.argv[1:]:
    try:
        f = open(arg, 'r')
    except IOError:
        print('cannot open', arg)
    else:
        print(arg, 'has', len(f.readlines()), 'lines')
        f.close()

4、異常的最終執行finally
try-finally 語句無論是否發生異常都將執行最後的程式碼。

定義清理行為:




s1 = 'hello'
try:
    int(s1)
except IndexError as e:
    print(e)
except KeyError as e:
    print(e)
except ValueError as e:
    print(e)
#except Exception as e:
#    print(e)
else:
    print('try內程式碼塊沒有異常則執行我')
finally:
    print('無論異常與否,都會執行該模組,通常是進行清理工作')

invalid literal for int() with base 10: 'hello'

無論異常與否,都會執行該模組,通常是進行清理工作

三、丟擲異常raise
Python 使用 raise 語句丟擲一個指定的異常。

raise語法格式如下:

raise [Exception [, args [, traceback]]]
try:
    raise TypeError('丟擲異常,型別錯誤')
except Exception as e:
    print(e)

raise 唯一的一個引數指定了要被丟擲的異常。它必須是一個異常的例項或者是異常的類(也就是 Exception 的子類)。

如果你只想知道這是否丟擲了一個異常,並不想去處理它,那麼一個簡單的 raise 語句就可以再次把它丟擲。

try:
        raise NameError('HiThere')
    except NameError:
        print('An exception flew by!')
        raise
   
#An exception flew by!
#Traceback (most recent call last):
#  File "", line 2, in ?
#NameError: HiThere

四、自定義異常
你可以通過建立一個新的異常類來擁有自己的異常。異常類繼承自 Exception 類,可以直接繼承,或者間接繼承,例如:

在這個例子中,類 Exception 預設的init() 被覆蓋。


class EgonException(Exception):
    def __init__(self, msg):
        self.msg = msg

    def __str__(self):
        return self.msg


try:
    raise EgonException('丟擲異常,型別錯誤')
except EgonException as e:
    print(e)
丟擲異常,型別錯誤

1、基礎異常類
當建立一個模組有可能丟擲多種不同的異常時,一種通常的做法是為這個包建立一個基礎異常類,然後基於這個基礎類為不同的錯誤情況建立不同的子類:

大多數的異常的名字都以"Error"結尾,就跟標準的異常命名一樣。

class Error(Exception):
    """Base class for exceptions in this module."""
    pass
 
class InputError(Error):
    """Exception raised for errors in the input.
 
    Attributes:
        expression -- input expression in which the error occurred
        message -- explanation of the error
    """
 
    def __init__(self, expression, message):
        self.expression = expression
        self.message = message
 
class TransitionError(Error):
    """Raised when an operation attempts a state transition that's not
    allowed.
 
    Attributes:
        previous -- state at beginning of transition
        next -- attempted new state
        message -- explanation of why the specific transition is not allowed
    """
 
    def __init__(self, previous, next, message):
        self.previous = previous
        self.next = next
        self.message = message



五、斷言assert
assert(斷言)用於判斷一個表示式,在表示式條件為 false 的時候觸發異常。

斷言可以在條件不滿足程式執行的情況下直接返回錯誤,而不必等待程式執行後出現崩潰的情況。

語法格式如下:

assert expression

等價於:

if not expression:
    raise AssertionError

assert 後面也可以緊跟引數:

assert expression [, arguments]

等價於:

if not expression:
    raise AssertionError(arguments)

以下例項判斷當前系統是否為 Linux,如果不滿足條件則直接觸發異常,不必執行接下來的程式碼:

import sys
assert ('linux' in sys.platform), "該程式碼只能在 Linux 下執行"
 接下來要執行的程式碼

# Traceback (most recent call last):
#    File "C:/PycharmProjects/untitled/run.py", line 2, in 
#      assert ('linux' in sys.platform), "該程式碼只能在 Linux 下執行"
#  AssertionError: 該程式碼只能在 Linux 下執行