Python_异常处理
1.try...except
stream = open('E:/projects/Python/test/filetry.txt','r') #读取 for line in stream: try: num=int(line.strip()) if(num==10): print('你真乖,拿的苹果数量整好!') elif(num>10): print('你不听话哦,拿多的请放回去!') else: print('你拿少了,请拿够你的苹果!') except Exception as e: print(e) continue stream.close()
执行结果:
你拿少了,请拿够你的苹果!
你真乖,拿的苹果数量整好!
invalid literal for int() with base 10: '15个'
你不听话哦,拿多的请放回去!