Python中異常(Exception)的總結

Python中的異常處理

異常處理的語句結構

try:
    <statements>        #運行try語句塊,並試圖捕獲異常
except <name1>:
    <statements>        #若是name1異常發現,那麼執行該語句塊。
except (name2, name3):
    <statements>        #若是元組內的任意異常發生,那麼捕獲它
except <name4> as <variable>:
    <statements>        #若是name4異常發生,那麼進入該語句塊,並把異常實例命名爲variable
except:
    <statements>        #發生了以上全部列出的異常以外的異常
else:
<statements>            #若是沒有異常發生,那麼執行該語句塊
finally:
    <statement>         #不管是否有異常發生,均會執行該語句塊。

說明python

  • else和finally是可選的,可能會有0個或多個except,可是,若是出現一個else的話,必須有至少一個except。
  • 無論你如何指定異常,異常老是經過實例對象來識別,而且大多數時候在任意給定的時刻激活。一旦異常在程序中某處由一條except子句捕獲,它就死掉了,除非由另外一個raise語句或錯誤從新引起它。

raise語句

raise語句用來手動拋出一個異常,有下面幾種調用格式:app

  • raise #能夠在raise語句以前建立該實例或者在raise語句中建立。
  • raise #Python會隱式地建立類的實例
  • raise name(value) #拋出異常的同時,提供額外信息value
  • raise # 把最近一次產生的異常從新拋出來
  • raise exception from E
    例如:
    拋出帶有額外信息的ValueError: raise ValueError('we can only accept positive values')

當使用from的時候,第二個表達式指定了另外一個異常類或實例,它會附加到引起異常的__cause__屬性。若是引起的異常沒有捕獲,Python把異常也做爲標準出錯消息的一部分打印出來:
好比下面的代碼:函數

try:
    1/0
except Exception as E:
    raise TypeError('bad input') from E

執行的結果以下:單元測試

Traceback (most recent call last):
  File "hh.py", line 2, in <module>
    1/0
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "hh.py", line 4, in <module>
    raise TypeError('bad input') from E
TypeError: bad input

assert語句

assert主要用來作斷言,一般用在單元測試中較多,到時候再作介紹。測試

with...as語句

with語句支持更豐富的基於對象的協議,能夠爲代碼塊定義支持進入和離開動做。
with語句對應的環境管理協議要求以下:ui

  • 環境管理器必須有__enter____exit__方法。
    __enter__方法會在初始化的時候運行,若是存在ass子在,__enter__函數的返回值會賦值給as子句中的變量,不然,直接丟棄。
    代碼塊中嵌套的代碼會執行。
    若是with代碼塊引起異常,__exit__(type,value,traceback)方法就會被調用(帶有異常細節)。這些也是由 sys.exc_info返回的相同值.若是此方法返回值爲假,則異常會從新引起。不然,異常會終止。正常 狀況下異常是應該被從新引起,這樣的話才能傳遞到with語句以外。
    若是with代碼塊沒有引起異常,__exit__方法依然會被調用,其type、value以及traceback參數都會以None傳遞。

下面爲一個簡單的自定義的上下文管理類。this

class Block:
    def __enter__(self):
        print('entering to the block')
        return self
    
    def prt(self, args):
        print('this is the block we do %s' % args)

    def __exit__(self,exc_type, exc_value, exc_tb):
        if exc_type is None:
            print('exit normally without exception')
        else:
            print('found exception: %s, and detailed info is %s' % (exc_type, exc_value))
        return False

with Block() as b:
    b.prt('actual work!')
    raise ValueError('wrong')

若是註銷到上面的raise語句,那麼會正常退出。
在沒有註銷掉該raise語句的狀況下,運行結果以下:spa

entering to the block
this is the block we do actual work!
found exception: <class 'ValueError'>, and detailed info is wrong
Traceback (most recent call last):
  File "hh.py", line 18, in <module>
    raise ValueError('wrong')
ValueError: wrong

異常處理器

若是發生異常,那麼經過調用sys.exc_info()函數,能夠返回包含3個元素的元組。 第一個元素就是引起異常類,而第二個是實際引起的實例,第三個元素traceback對象,表明異常最初發生時調用的堆棧。若是一切正常,那麼會返回3個None。code

Python的Builtins模塊中定義的Exception

|Exception Name|Description|
|BaseException|Root class for all exceptions|
|   SystemExit|Request termination of Python interpreter|
|KeyboardInterrupt|User interrupted execution (usually by pressing Ctrl+C)|
|Exception|Root class for regular exceptions|
|   StopIteration|Iteration has no further values|
|   GeneratorExit|Exception sent to generator to tell it to quit|
|   SystemExit|Request termination of Python interpreter|
|   StandardError|Base class for all standard built-in exceptions|
|       ArithmeticError|Base class for all numeric calculation errors|
|           FloatingPointError|Error in floating point calculation|
|           OverflowError|Calculation exceeded maximum limit for numerical type|
|           ZeroDivisionError|Division (or modulus) by zero error (all numeric types)|
|       AssertionError|Failure of assert statement|
|       AttributeError|No such object attribute|
|       EOFError|End-of-file marker reached without input from built-in|
|       EnvironmentError|Base class for operating system environment errors|
|           IOError|Failure of input/output operation|
|           OSError|Operating system error|
|               WindowsError|MS Windows system call failure|
|               ImportError|Failure to import module or object|
|               KeyboardInterrupt|User interrupted execution (usually by pressing Ctrl+C)|
|           LookupError|Base class for invalid data lookup errors|
|               IndexError|No such index in sequence|
|               KeyError|No such key in mapping|
|           MemoryError|Out-of-memory error (non-fatal to Python interpreter)|
|           NameError|Undeclared/uninitialized object(non-attribute)|
|               UnboundLocalError|Access of an uninitialized local variable|
|           ReferenceError|Weak reference tried to access a garbage collected object|
|           RuntimeError|Generic default error during execution|
|               NotImplementedError|Unimplemented method|
|           SyntaxError|Error in Python syntax|
|               IndentationError|Improper indentation|
|                   TabErrorg|Improper mixture of TABs and spaces|
|           SystemError|Generic interpreter system error|
|           TypeError|Invalid operation for type|
|           ValueError|Invalid argument given|
|               UnicodeError|Unicode-related error|
|                   UnicodeDecodeError|Unicode error during decoding|
|                   UnicodeEncodeError|Unicode error during encoding|
|                   UnicodeTranslate Error|Unicode error during translation|
|       Warning|Root class for all warnings|
|           DeprecationWarning|Warning about deprecated features|
|           FutureWarning|Warning about constructs that will change semantically in the future|
|           OverflowWarning|Old warning for auto-long upgrade|
|           PendingDeprecation Warning|Warning about features that will be deprecated in the future|
|           RuntimeWarning|Warning about dubious runtime behavior|
|           SyntaxWarning|Warning about dubious syntax|
|           UserWarning|Warning generated by user code|
相關文章
相關標籤/搜索