Python 异常处理

 基础的处理

一个简单的小例子

 



1 import time 2 try: 3 time.sleep(2) # 程序延时2秒 4 open("xxx.txt") 5 print("111111111111111") 6 7 8 except (NameError, FileNotFoundError): 9 print('*-*-*') 10 except Exception as ret: 11 print("如果有异常,并不在except中,执行(捕获所有异常)") 12 print(ret)#打印异常类型 13 else: 14 print("没有异常执行") 15 finally: 16 print("产没产生异常都执行") 17 18 print("结束")

 结果

1 *-*-*
2 产没产生异常都执行
3 结束