一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 關于Python中異常(Exception)的匯總

關于Python中異常(Exception)的匯總

2020-09-18 10:30jessonsh Python

異常是指程序中的例外,違例情況。異常機制是指程序出現錯誤后,程序的處理方法。當出現錯誤后,程序的執行流程發生改變,程序的控制權轉移到異常處理。下面這篇文章主要匯總了關于Python中異常(Exception)的相關資料,需要的

前言

Exception類是常用的異常類,該類包括StandardError,StopIteration, GeneratorExit, Warning等異常類。python中的異常使用繼承結構創建,可以在異常處理程序中捕獲基類異常,也可以捕獲各種子類異常,python中使用try...except語句捕獲異常,異常子句定義在try子句后面。

Python中的異常處理

異常處理的語句結構

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
try:
 <statements>  #運行try語句塊,并試圖捕獲異常
except <name1>:
 <statements>  #如果name1異常發現,那么執行該語句塊。
except (name2, name3):
 <statements>  #如果元組內的任意異常發生,那么捕獲它
except <name4> as <variable>:
 <statements>  #如果name4異常發生,那么進入該語句塊,并把異常實例命名為variable
except:
 <statements>  #發生了以上所有列出的異常之外的異常
else:
<statements>   #如果沒有異常發生,那么執行該語句塊
finally:
 <statement>   #無論是否有異常發生,均會執行該語句塊。

說明

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

raise語句

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

  • 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把異常也作為標準出錯消息的一部分打印出來:

比如下面的代碼:

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

執行的結果如下:

?
1
2
3
4
5
6
7
8
9
10
11
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語句對應的環境管理協議要求如下:

  • 環境管理器必須有__enter____exit__方法。

       __enter__方法會在初始化的時候運行,如果存在ass子在, __enter__函數的返回值會賦值給as子句中的變量,否則,直接丟棄。

       代碼塊中嵌套的代碼會執行。

       如果with代碼塊引發異常, __exit__(type,value,traceback)方法就會被調用(帶有異常細節)。這些也是由 sys.exc_info返回的相同值.如果此方法返回值為假,則異常會重新引發。否則,異常會終止。正常 情況下異常是應該被重新引發,這樣的話才能傳遞到with語句之外。

       如果with代碼塊沒有引發異常, __exit__方法依然會被調用,其type、value以及traceback參數都會以None傳遞。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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語句的情況下,運行結果如下:

?
1
2
3
4
5
6
7
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。

Python的Builtins模塊中定義的Exception

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|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|

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

原文鏈接:http://www.cnblogs.com/jessonluo/p/4743574.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜片无码区在线观看 | 久久国产精品人妻中文 | 羞羞漫画视频 | 亚洲男人天堂 | 1024国产精品视频观看 | 国产精品欧美韩国日本久久 | 免费网站看v片在线成人国产系列 | 3d蒂法受辱在线播放 | 国产日韩欧美在线播放 | 洗濯屋H纯肉动漫在线观看 武侠艳妇屈辱的张开双腿 午夜在线观看免费观看 视频 | 亚洲国产欧美久久香综合 | 亚洲国产韩国欧美在线不卡 | 调教女警花穿环上班 | 男女男精品视频免费观看 | 欧美日韩国产亚洲一区二区 | 日朝欧美亚洲精品 | 99精品视频免费 | 色戒完整版2小时38分钟 | 亚洲精品青青草原avav久久qv | 娇妻中日久久持久久 | 美女任你模 | 公交车上插入 | 国产精品va在线观看不 | 2019午夜福合集高清完整版 | fistingvideos头交尿眼 | 国产精品全国探花在线观看 | 精品视频久久久久 | 女人和男人搞基 | 久久久精品国产免费A片胖妇女 | 91精品国产免费久久国语蜜臀 | 羞羞视频动漫 | caonila国产在线观看 | 免费一级欧美片在线观免看 | 四虎影库紧急大通知 | 日本国产最新一区二区三区 | 亚洲国产高清一区二区三区 | 四虎com | 欧美亚洲欧美 | 国产一区二区三区在线观看视频 | 欧美四级无删版影片 | 国产精品色爱综合网 |