码迷,mamicode.com
首页 > 编程语言 > 详细

python 中异常处理

时间:2016-09-18 15:34:53      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:python   except   invalid   raised   

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 中异常处理

标签:python   except   invalid   raised   

原文地址:http://7956322.blog.51cto.com/7946322/1853629

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!