单一
names = ['sooele','cooi'] data = {} # data['name'] try: #尝试 # names[3] #列表操作错误 # data['name'] #没有这个key #open("tes.txt") #未知错误 a = 1 #一切正常 print(a) #一切正常 except (KeyError,IndexError) as e: #除KeyError外 print("没有这个key",e) except IndexError as e: print("列表操作错误") except Exception as e: print("未知错误",e) else: print("一切正常") finally: #最后,都执行 print("不管有错没错都执行")
多行判断:
names = ['sooele','cooi'] data = {} # data['name'] try: #尝试 names[3] #列表操作错误 data['name'] #没有这个key except (KeyError,IndexError) as e: #除KeyError外 print("没有这个key",e) #except IndexError as e: # print("列表操作错误")