Python报错不要慌,这三个关键词帮你解决问题!

开发 后端
写代码必然会出现错误,而错误处理可以针对这些错误提前做好准备。通常出现错误时,脚本会停止运行,而有了错误处理,脚本就可以继续运行。

本文转载自公众号“读芯术”(ID:AI_Discovery)。

写代码必然会出现错误,而错误处理可以针对这些错误提前做好准备。通常出现错误时,脚本会停止运行,而有了错误处理,脚本就可以继续运行。为此,我们需要了解下面三个关键词:

  • try:这是要运行的代码块,可能会产生错误。
  • except:如果在try块中出现错误,将执行这段代码。
  • finally:不管出现什么错误,都要执行这段代码。

[[352683]]

现在,我们定义一个函数“summation”,将两个数字相加。该函数运行正常。

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

接下来,我们让用户输入其中一个数字,并运行该函数。

  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”错误出现了,因为我们试图将数字和字符串相加。请注意,错误出现后,后面的代码便不再执行。所以我们要用到上面提到的关键词,确保即使出错,脚本依旧运行。

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

可以看到,try块出现错误,except块的代码开始运行,并打印语句。接下来加入“else”块,来应对没有错误出现的情况。

  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 

接下来我们用另外一个例子理解。这个例子中,在except块我们还标明了错误类型。如果没有标明错误类型,出现一切异常都会执行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 

 

现在,故意创造一个错误,看看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 

 

责任编辑:赵宁宁 来源: 读芯术
相关推荐

2011-06-27 17:07:35

SEO

2017-11-02 13:15:18

Linux

2011-06-02 18:27:06

关键词标题

2011-06-20 14:32:59

关键词

2020-10-20 06:45:48

编程高并发

2016-11-01 14:33:18

郝联峰大数据

2011-06-07 18:45:41

关键词

2015-07-27 11:11:31

混合云混合云管理云服务

2018-05-28 14:38:44

PHPPython应用

2020-03-25 15:07:21

数据挖掘大数据思维

2011-06-14 19:11:38

关键词

2015-07-20 10:17:37

云计算应用混合云混合云管理

2011-06-21 16:11:04

SEO关键词

2011-06-16 17:54:25

关键词

2013-08-26 15:43:40

AppStore关键词开发者应用选取关键词

2011-05-25 17:58:00

2019-12-22 13:48:26

退休科技行业大佬

2011-05-25 17:38:56

关键词

2011-06-07 17:50:20

网站权重

2021-12-23 10:05:43

机器学习人工智能黑盒模型
点赞
收藏

51CTO技术栈公众号