天天看点

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:

上一篇: 二进制