天天看點

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:

上一篇: 二進制