Python:异常处理(try .. except)
如下所示的代码演示了Python中的异常处理:
1 2 3 4 5 6 7 8 9 |
__author__ = 'adamhuan' dictionary = {"Girl":"Angela Baby"} try: print dictionary['Girl'] print dictionary['wife'] except KeyError,e: print "The Key is not existed." |
它的运行效果是这样的:
1 2 3 4 5 |
C:\Python27\python.exe D:/adamhuan_data/Python_Project/Django_TEMP/demo1.py Angela Baby The Key is not existed. Process finished with exit code 0 |
————————————————————————
Done。