Python3物件導向 3 異常(3)異常作用

2021-10-07 15:42:02 字數 1307 閱讀 8619

當丟擲異常時,看起來好像立即停止了程式的執行。丟擲異常之後的所有**都不會執行,除非處理了這一異常,程式將會退出並給出錯誤資訊。

def

no_return()

:print

("i am about to raise an exception"

)raise exception(

"this is always raised"

)print

("this line will never execute"

)return

"i would not be reutrned"

no_return(

)traceback (most recent call last)

:i am about to raise an exception

file "d:/untitled1/book/venv/異常7.py"

, line 6,in

no_return(

) file "d:/untitled1/book/venv/異常7.py"

, line 3

,in no_return

raise exception(

"this is always raised"

)exception: this is always raised

丟擲異常會阻止函式棧中所有的執行

defc(

):print

("1"

) no_return(

)"呼叫異常函式"

print

("2")c(

)1traceback (most recent call last)

:i am about to raise an exception

file "d:/untitled1/book/venv/異常7.py"

, line 12,in

c() file "d:/untitled1/book/venv/異常7.py"

, line 9

,in c

no_return(

) file "d:/untitled1/book/venv/異常7.py"

, line 3

,in no_return

raise exception(

"this is always raised"

)exception: this is always raised

python3物件導向

類 class 描述同屬性和方法的物件的集合。方法 類中定義的函式 例項化 建立乙個類的例項,類的具體物件。物件 通過類定的資料例。包括兩個資料成員 類變數和例項變數 和方法。支援操作 屬性引用和例項化 class myclass i 12345 deff self return hello wor...

Python3 物件導向

類名 這類事物的名字,滿足大駝峰命名法,每乙個單詞的首字母大寫 屬性 這類事物具有什麼樣的特徵,身高,體重,年齡等 方法 這類事物具有什麼樣的行為,會跑,會說話,會跳等 class cat 名字貓的類,建議用大駝峰 def init self,new name,new age self.name n...

python3物件導向

類的定義 class classname name 屬性 age 18 私有屬性 deff self 方法,self 代表的是類的例項 return hello world def say self 私有方法,self 代表的是類的例項 print hello world 例項化 x classna...