异常处理
异常处理
# a[4] # try: # '1'+ '2' # 2 / 1 # except TypeError as e: # print(e) # print('出错了') # except ZeroDivisionError as e: # print('除数不能为0') # except IndexError: # print('下标错误') # except KeyError: # print('找不到字典的key') # except FileNotFoundError: # print('文件打不开') # except Exception as e: # print('出异常了',e) # else: # print('没有出异常') # finally: # print('aaa') # f = open('xxx','w') # try: # f.write() # except: # print('走这块') # else: # print('xxx') # finally: # f.close() import traceback def is_price(s): try: f = float(s) except Exception as e: # traceback.print_exc() print('zijijiade .....',traceback.format_exc()) return False else: if f>0: return True is_price('sdgsdf') #调用堆栈信息
money = 1000 num = input('plese enter a num:') try: num = float(num) res = money/num except Exception as e: #出现异常了,就走except下面的代码 # except ValueError,e: #python2 print('出现异常了') print('你输入的价格不合法。。') print(e) else:#没有出现异常 money-=num print(num) print('你的余额是%s'%money) finally: print('我是finally') class QqGroup: cookie = {'cookie':'xx'} def get_all_gorup(self): return {'xxx':2342343,'xxx':2323} def save_mongo(self,data): pass def get_group_info(self,num): return {''} def down_pic(self,num): pass def main(self):#入 pass