第19节--python常见错误与异常处理
2016-10-31 11:36
232 查看
语法错误
三种异常
异常处理
1)首先,try语句下的(try和except之间的代码)被执行;
2)如果没有出现异常,except语句将被忽略;
3)如果try语句之间出现了异常,try之下异常之后的代码被忽略,直接跳跃到except语句;
4)如果异常出现,但并不属于except中定义的异常类型,程序将执行外围一层的try语句,如果异常没有被处理,将产生unhandled exception的错误。
code
捕捉两种异常
异常处理参考文档:docs.python.org/3.4/library/exceptions.html
# while True print("hello world")
三种异常
# print(8/0) # print(hello * 4) # num = 6 # print("hello" + num)
异常处理
# while True: # try: # x = int(input("Please enter a numer: ")) # break # except ValueError: # print("Not valid input, input again...")
1)首先,try语句下的(try和except之间的代码)被执行;
2)如果没有出现异常,except语句将被忽略;
3)如果try语句之间出现了异常,try之下异常之后的代码被忽略,直接跳跃到except语句;
4)如果异常出现,但并不属于except中定义的异常类型,程序将执行外围一层的try语句,如果异常没有被处理,将产生unhandled exception的错误。
code
捕捉两种异常
# try: # f = open('myfile.txt') # s = f.readline() # i = int(s.strip()) # except OSError as err: # print("OS error is {}".format(err)) # except ValueError: # print("Could not convert data to an integer") # print(i)
异常处理参考文档:docs.python.org/3.4/library/exceptions.html
相关文章推荐
- 17个新手常见Python运行时错误
- Python运行的17个时新手常见错误小结
- 17个新手常见Python运行时错误
- 17个新手常见Python运行时错误
- 17个新手常见Python运行时错误
- python 弱类型语言常见的错误
- 17个新手常见Python运行时错误
- 17个新手常见Python运行时错误
- python中tkinter中新手一个常见的错误
- 17个新手常见Python运行时错误
- Python运行的17个时新手常见错误小结
- 17个新手常见Python运行时错误
- python 常见面试问题(1)-python 输出中文问题/求list的交集或差集/一个简单的socket编程/python 异常处理
- 17个常见Python运行时错误
- Python 常见错误
- 17个新手常见Python运行时错误
- Python常见错误情况
- 初学Python常见的错误
- 17个新手常见Python运行时错误
- 17个新手常见Python运行时错误