本文转载自公众号“读芯术”(ID:AI_Discovery)。
写代码必然会出现错误,而错误处理可以针对这些错误提前做好准备。通常出现错误时,脚本会停止运行,而有了错误处理,脚本就可以继续运行。为此,我们需要了解下面三个关键词:
现在,我们定义一个函数“summation”,将两个数字相加。该函数运行正常。
- >>> defsummation(num1,num2):
- print(num1+num2)>>>summation(2,3)
- 5
接下来,我们让用户输入其中一个数字,并运行该函数。
- >>> num1 = 2
- >>> num2 = input("Enter number: ")
- Enter number: 3>>> summation(num1,num2)>>> print("Thisline will not be printed because of the error")
- ---------------------------------------------------------------------------
- TypeError Traceback (most recent call last)
- <ipython-input-6-2cc0289b921e> in <module>
- ----> 1 summation(num1,num2)
- 2 print("This line will notbe printed because of the error")
- <ipython-input-1-970d26ae8592> in summation(num1, num2)
- 1 def summation(num1,num2):
- ----> 2 print(num1+num2)
- TypeError: unsupported operand type(s) for +: int and str
“TypeError”错误出现了,因为我们试图将数字和字符串相加。请注意,错误出现后,后面的代码便不再执行。所以我们要用到上面提到的关键词,确保即使出错,脚本依旧运行。
- >> try:
- summed = 2 + 3
- except:
- print("Summation is not ofthe same type")Summation is not of the same type
可以看到,try块出现错误,except块的代码开始运行,并打印语句。接下来加入“else”块,来应对没有错误出现的情况。
- >>> try:
- summed = 2 + 3
- except:
- print("Summation is not ofthe same type")
- else:
- print("There was no errorand result is: ",summed)There was no error and result is: 5
接下来我们用另外一个例子理解。这个例子中,在except块我们还标明了错误类型。如果没有标明错误类型,出现一切异常都会执行except块。
- >>> try:
- f = open( test , w )
- f.write("This is a testfile")
- except TypeError:
- print("There is a typeerror")
- except OSError:
- print("There is an OSerror")
- finally:
- print("This will print evenif no error")This will print even if no error
现在,故意创造一个错误,看看except块是否与finally块共同工作吧!
- >>> try:
- f = open( test , r )
- f.write("This is a testfile")
- except TypeError:
- print("There is a typeerror")
- except OSError:
- print("There is an OSerror")
- finally:
- print("This will print evenif no error")There is an OS error
- This will print even if no error
数澜科技是一家数据中台服务独立供应商 成立5年 已为1000余家企业、政府机构提供...
域名 命名实名中什么意思?根据《中国互联网络域名管理办法》的规定, 域名注册 ...
近年来,数据越来越成为重要的基础性战略资源,并逐渐显现出生产要素的属性,并...
如果您的Spring Cloud集群(包含多个应用)已经部署在阿里云上,您可以将应用迁...
云计算已成为发展数字经济的重要引擎。全球云计算市场规模不断扩大,并呈多样化...
队列是一种特殊的线性表,是一种先进先出(FIFO)的数据结构。它只允许在表的前端(...
租用服务器 稳定运行的注意事项 服务器租用 是网站建设常用方式之一,很多站长认...
操作场景 本文以使用 WordPress 5.4.2 社区版应用镜像 的轻量应用服务器为例,介...
调用DescribeDedicatedHostClusters查询一个或多个专有宿主机集群的详细信息。 ...
每个镜像最多可以共享给多少个租户? 系统盘镜像和数据盘镜像为128个;整机镜像...