首页 > 代码库 > python 中异常处理
python 中异常处理
Python中的异常由 try-except [exceptionname] 块处理,例如:
def some_function():
try:
# Division by zero raises an exception
10 / 0
except ZeroDivisionError:
print "Oops, invalid."
else:
# Exception didn‘t occur, we‘re good.
pass
finally:
# This is executed after the code block is run
# and all exceptions have been handled, even
# if a new exception is raised while handling.
print "We‘re done with that."
>>> some_function()
Oops, invalid.
We‘re done with that.
try:
except:
else:
finally: 不管触不触发except都要执行的这一步
python 中异常处理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。