自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python報(bào)錯(cuò)不要慌,這三個(gè)關(guān)鍵詞幫你解決問題!

開發(fā) 后端
寫代碼必然會(huì)出現(xiàn)錯(cuò)誤,而錯(cuò)誤處理可以針對(duì)這些錯(cuò)誤提前做好準(zhǔn)備。通常出現(xiàn)錯(cuò)誤時(shí),腳本會(huì)停止運(yùn)行,而有了錯(cuò)誤處理,腳本就可以繼續(xù)運(yùn)行。

本文轉(zhuǎn)載自公眾號(hào)“讀芯術(shù)”(ID:AI_Discovery)。

寫代碼必然會(huì)出現(xiàn)錯(cuò)誤,而錯(cuò)誤處理可以針對(duì)這些錯(cuò)誤提前做好準(zhǔn)備。通常出現(xiàn)錯(cuò)誤時(shí),腳本會(huì)停止運(yùn)行,而有了錯(cuò)誤處理,腳本就可以繼續(xù)運(yùn)行。為此,我們需要了解下面三個(gè)關(guān)鍵詞:

  • try:這是要運(yùn)行的代碼塊,可能會(huì)產(chǎn)生錯(cuò)誤。
  • except:如果在try塊中出現(xiàn)錯(cuò)誤,將執(zhí)行這段代碼。
  • finally:不管出現(xiàn)什么錯(cuò)誤,都要執(zhí)行這段代碼。

[[352683]]

現(xiàn)在,我們定義一個(gè)函數(shù)“summation”,將兩個(gè)數(shù)字相加。該函數(shù)運(yùn)行正常。

  1. >>> defsummation(num1,num2): 
  2.         print(num1+num2)>>>summation(2,3) 

接下來,我們讓用戶輸入其中一個(gè)數(shù)字,并運(yùn)行該函數(shù)。

  1. >>> num1 = 2 
  2. >>> num2 = input("Enter number: ") 
  3. Enter number: 3>>> summation(num1,num2)>>> print("Thisline will not be printed because of the error") 
  4. --------------------------------------------------------------------------- 
  5. TypeError                                Traceback (most recent call last) 
  6. <ipython-input-6-2cc0289b921e> in <module> 
  7. ----> 1 summation(num1,num2) 
  8.       2 print("This line will notbe printed because of the error") 
  9.  
  10. <ipython-input-1-970d26ae8592> in summation(num1, num2) 
  11.       1 def summation(num1,num2): 
  12. ----> 2     print(num1+num2) 
  13.  
  14. TypeError: unsupported operand type(s) for +:  int  and  str  

“TypeError”錯(cuò)誤出現(xiàn)了,因?yàn)槲覀冊(cè)噲D將數(shù)字和字符串相加。請(qǐng)注意,錯(cuò)誤出現(xiàn)后,后面的代碼便不再執(zhí)行。所以我們要用到上面提到的關(guān)鍵詞,確保即使出錯(cuò),腳本依舊運(yùn)行。

  1. >> try: 
  2.         summed = 2 +  3  
  3.     except: 
  4.         print("Summation is not ofthe same type")Summation is not of the same type 

可以看到,try塊出現(xiàn)錯(cuò)誤,except塊的代碼開始運(yùn)行,并打印語句。接下來加入“else”塊,來應(yīng)對(duì)沒有錯(cuò)誤出現(xiàn)的情況。

  1. >>> try: 
  2.         summed = 2 + 3 
  3.     except: 
  4.         print("Summation is not ofthe same type") 
  5.     else: 
  6.         print("There was no errorand result is: ",summed)There was no error and result is:  5 

接下來我們用另外一個(gè)例子理解。這個(gè)例子中,在except塊我們還標(biāo)明了錯(cuò)誤類型。如果沒有標(biāo)明錯(cuò)誤類型,出現(xiàn)一切異常都會(huì)執(zhí)行except塊。

  1. >>> try: 
  2.         f = open( test , w ) 
  3.         f.write("This is a testfile") 
  4.     except TypeError: 
  5.         print("There is a typeerror") 
  6.     except OSError: 
  7.         print("There is an OSerror") 
  8.     finally: 
  9.         print("This will print evenif no error")This will print even if no error 

 

現(xiàn)在,故意創(chuàng)造一個(gè)錯(cuò)誤,看看except塊是否與finally塊共同工作吧!

  1. >>> try: 
  2.         f = open( test , r ) 
  3.         f.write("This is a testfile") 
  4.     except TypeError: 
  5.         print("There is a typeerror") 
  6.     except OSError: 
  7.         print("There is an OSerror") 
  8.     finally: 
  9.         print("This will print evenif no error")There is an OS error 
  10. This will print even if no error 

 

責(zé)任編輯:趙寧寧 來源: 讀芯術(shù)
相關(guān)推薦

2011-06-27 17:07:35

SEO

2017-11-02 13:15:18

Linux

2011-06-02 18:27:06

關(guān)鍵詞標(biāo)題

2011-06-20 14:32:59

關(guān)鍵詞

2020-10-20 06:45:48

編程高并發(fā)

2016-11-01 14:33:18

郝聯(lián)峰大數(shù)據(jù)

2015-07-27 11:11:31

混合云混合云管理云服務(wù)

2011-06-07 18:45:41

關(guān)鍵詞

2020-03-25 15:07:21

數(shù)據(jù)挖掘大數(shù)據(jù)思維

2018-05-28 14:38:44

PHPPython應(yīng)用

2015-07-20 10:17:37

云計(jì)算應(yīng)用混合云混合云管理

2011-06-14 19:11:38

關(guān)鍵詞

2011-06-16 17:54:25

關(guān)鍵詞

2013-08-26 15:43:40

AppStore關(guān)鍵詞開發(fā)者應(yīng)用選取關(guān)鍵詞

2011-05-25 17:58:00

2011-06-21 16:11:04

SEO關(guān)鍵詞

2021-12-23 10:05:43

機(jī)器學(xué)習(xí)人工智能黑盒模型

2020-10-09 11:30:07

Redis緩存數(shù)據(jù)庫(kù)

2011-05-25 17:38:56

關(guān)鍵詞

2019-12-22 13:48:26

退休科技行業(yè)大佬
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)