热线电话:13121318867

登录
2019-02-27 阅读量: 603
代码报错时如何执行清理命令?

代码如果正确,会直接执行清理命令,如果代码引发错误,会在except子句进行处理,最后才会采取清理命令。

# Python code to illustrate

# clean up actions

def divide(x, y):

try:

# Floor Division : Gives only Fractional Part as Answer

result = x // y

except ZeroDivisionError:

print("Sorry ! You are dividing by zero ")

else:

print("Yeah ! Your answer is :", result)

finally:

print("I'm finally clause, always raised !! ")

# Look at parameters and note the working of Program

divide(3, "3")

Output :

I'm finally clause, always raised !! 

Error:

Traceback (most recent call last):
File "C:/Users/DELL/Desktop/Code.py", line 15, in
divide(3, "3")
File "C:/Users/DELL/Desktop/Code.py", line 7, in divide
result = x // y
TypeError: unsupported operand type(s) for //: 'int' and 'str'
0.0000
4
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子