标签:nal index ice 错误 not sdi 判断 pre 价格
1 a = [1,2,3] 2 d = {} 3 #例子1: 4 #判断key异常 5 try: 6 d[‘name‘] 7 except KeyError as e: 8 print("字典key不存在",e) 9 else: 10 print(‘正常运行‘) 11 finally: 12 print("有没有异常都走这里") 13 14 #例子2 15 #判断下标异常 16 try: 17 a[3] 18 except IndexError as e: 19 print(‘下标越界‘) 20 else: 21 print(‘正常运行‘) 22 finally: 23 print(‘有没有异常都走这里‘) 24 25 # 例子3 26 #捕捉全部异常 27 try: 28 a[3] 29 d[‘name‘] 30 except Exception as e: 31 print(‘出现异常了‘) 32 finally: 33 print(‘有没有异常都走这里‘) 34 35 #raise主动抛出异常 36 price = input(‘price:‘) 37 if not price.isdigit(): 38 raise Exception(‘价格错误‘)
标签:nal index ice 错误 not sdi 判断 pre 价格
原文地址:https://www.cnblogs.com/xumb/p/11963410.html