py python異常處理

2021-08-07 12:08:48 字數 1420 閱讀 2478

try:

num=int('asda')

print(num)

except indexerror as e:

print("chucuola")

except exception as e:

print(e)

else:

print("helo")

finally:

print("the pro is end")

手動觸發異常

try:

num=int(12)

print(num)

raise importerror

except indexerror as e:

print("chucuola")

except exception as e:

print("import error")

else:

print("helo")

finally:

print("the pro is end")

import error

the pro is

end執行路徑: **報錯–except–finally

需求1:

持續輸入:

直接回車,不算

ctrl+c無法退出

輸入的是非數字

已上這三種異常均需要撲捉並提示try again

一共有3次猜測機會

num = 0

while

true:

try:

if(int(input("a num > "))) == 100:

print("correct")

break

if num == 3:

print("th coreect num is 100")

else:

print("try again")

num += 1

except(keyboardinterrupt,ioerror,valueerror):

print("pls input a num")

需求2:

輸入n後退出

輸入n***x也退出

其他輸入均表示繼續

while

true:

try:

op = input("again?[y]").lower()

if op and op[0]=="n":

break

except(keyboardinterrupt,eoferror):

print("pls input a y/n")

十以內加法訓練器

struts 異常處理 全域性異常處理

記錄一下全域性異常處理的過程 處理主動丟擲的異常,轉向錯誤提示頁面。1 寫乙個自己的異常,繼承runtimeexception,從父類生成構造方法 package me.yndy.srtp.exception suppresswarnings serial public class errorexc...

python異常處理 Python 異常處理

使用者輸入不完整 比如輸入為空 或者輸入非法 輸入不是數字 異常就是程式執行時發生錯誤的訊號,在python中,錯誤觸發的異常如下 在python中不同的異常可以用不同的型別 python中統一了類與型別,型別即類 去標識,不同的類物件標識不同的異常,乙個異常標識一種錯 觸發indexerror 觸...

01 異常 異常處理

注意 1 對定義的變數能初始化的盡量初始化,如果賦的值可能產生誤導,可以採用可空型別 例如 double?dnumres null 3 異常 執行的時候出現的錯誤,或bug 編寫 的預期條件玉實際條件不穩合 異常的機制是為了保證程式可以一直正常執行。丟擲異常 throw new exception ...