首页 > 代码库 > 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 中异常处理