try: dic = {'a': 1,} dic['b'] # KeyError lst = ['a', 'b'] lst[10] # IndexError s = "2GB" s = int(s) # ValueError except ValueError as e: # e为异常的值 print(e) except KeyError as e: print(e) except IndexError as e: print(e) except Exception as e: # 万能异常 print(e) else: print("没有异常会执行我.") # 一般用来关闭文件等操作 finally: print("有没有异常都会执行我") # 异常包括三部分: 追踪信息/异常类型/异常的值 # try: # 被检测的代码块 # except 异常类型: # try中一旦检测到异常,就执行这个位置的逻辑