Python語法?raise?...?from?用法示例詳解
前言
當(dāng)程序出現(xiàn)錯誤時,系統(tǒng)會自動觸發(fā)異常。Python 也允許程序自行引發(fā)異常,自行引發(fā)異常使用 raise 語句來完成。
- 使用
raise拋出新的異常 - 使用
raise ... from ...拋出新的異常時,新的異常是由舊的異常表現(xiàn)的; - 使用
raise ... from None拋出新的異常時,不會打印舊的異常(即禁止的異常關(guān)聯(lián))
raise 引發(fā)異常
使用 raise 語句,主動引發(fā)異常,終止程序
x = 20
if not isinstance(x, str):
raise Exception("value is not type of str")
else:
print("hello")運(yùn)行結(jié)果:
Traceback (most recent call last):
File "D:/demo/untitled1/demo/a.py", line 4, in <module>
raise Exception("value is not type of str")
Exception: value is not type of str
當(dāng)raise 拋出異常時,程序就會終止
try... except 捕獲異常
使用try... except 捕獲異常
x = [20, 3, 22, 11]
try:
print(x[7])
except IndexError:
print("index out of list")運(yùn)行后不會有異常
在捕獲異常后,也可以重新拋一個其它的異常
x = [20, 3, 22, 11]
try:
print(x[7])
except IndexError:
print("index out of list")
raise NameError("new exception ...")輸出結(jié)果:
Traceback (most recent call last):
File "D:/demo/untitled1/demo/a.py", line 4, in <module>
print(x[7])
IndexError: list index out of rangeDuring handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/demo/untitled1/demo/a.py", line 7, in <module>
raise NameError("new exception ...")
NameError: new exception ...
在拋出異常的日志中,可以看到日志中對 IndexError 和 NameError之間是描述是 During handling of the above exception, another exception occurred,即在處理 IndexError 異常時又出現(xiàn)了 NameError 異常,兩個異常之間沒有因果關(guān)系。
raise ... from 用法
示例:
x = [20, 3, 22, 11]
try:
print(x[7])
except IndexError as e:
print("index out of list")
raise NameError("new exception ...") from e運(yùn)行結(jié)果
Traceback (most recent call last):
File "D:/demo/untitled1/demo/a.py", line 4, in <module>
print(x[7])
IndexError: list index out of rangeThe above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:/demo/untitled1/demo/a.py", line 7, in <module>
raise NameError("new exception ...") from e
NameError: new exception ...
在拋出異常的日志中,可以看到日志中對 IndexError和 NameError 之間是描述是 The above exception was the direct cause of the following exception,即因?yàn)?IndexError 直接異常導(dǎo)致了 NameError異常,兩個異常之間有直接因果關(guān)系。
示例:
x = [20, 3, 22, 11]
try:
print(x[7])
except IndexError as e:
print("index out of list")
raise NameError("new exception ...") from None運(yùn)行結(jié)果
Traceback (most recent call last):
File "D:/demo/untitled1/demo/a.py", line 7, in <module>
raise NameError("new exception ...") from None
NameError: new exception ...
在拋出異常的日志中,可以看到日志只打印了 NameError 而沒有打印 IndexError。
補(bǔ)充:python中異常處理--raise的使用
使用raise拋出異常
當(dāng)程序出現(xiàn)錯誤,python會自動引發(fā)異常,也可以通過raise顯示地引發(fā)異常。一旦執(zhí)行了raise語句,raise后面的語句將不能執(zhí)行。
演示raise用法
try:
s = None
if s is None:
print "s 是空對象"
raise NameError #如果引發(fā)NameError異常,后面的代碼將不能執(zhí)行
print len(s) #這句不會執(zhí)行,但是后面的except還是會走到
except TypeError:
print "空對象沒有長度"
s = None
if s is None:
raise NameError
print 'is here?' #如果不使用try......except這種形式,那么直接拋出異常,不會執(zhí)行到這里到此這篇關(guān)于Python語法 raise ... from 用法示例詳解的文章就介紹到這了,更多相關(guān)Python raise ... from 用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python pyecharts Line折線圖的具體實(shí)現(xiàn)
折線圖在很多圖標(biāo)中都有使用,本文主要介紹了Python pyecharts Line折線圖的具體實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
淺談pandas關(guān)于查看庫或依賴庫版本的API原理
本文主要介紹了淺談pandas關(guān)于查看庫或依賴庫版本的API原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
python如何將文件a.txt的內(nèi)容復(fù)制到b.txt中
這篇文章主要介紹了python如何將文件a.txt的內(nèi)容復(fù)制到b.txt中,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
使用OpenCV獲取圖片連通域數(shù)量,并用不同顏色標(biāo)記函
這篇文章主要介紹了使用OpenCV獲取圖片連通域數(shù)量,并用不同顏色標(biāo)記函,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python實(shí)現(xiàn)帶下標(biāo)索引的遍歷操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)帶下標(biāo)索引的遍歷操作,結(jié)合具體實(shí)例形式分析了2種帶索引的遍歷操作實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-05-05

