|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 人若有情死得早 于 2017-7-9 22:55 编辑
1.try-except语句
try:
检测范围
except Exception[as reason]:
出现异常(Exception)后的处理代码
1)代码:
- f = open('我为什么是一个文件.txt')
- print(f.read())
- f.close()
- ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
- Traceback (most recent call last):
- File "C:\Users\Administrator\Desktop\test.py", line 1, in <module>
- f = open('我为什么是一个文件.txt')
- FileNotFoundError: [Errno 2] No such file or directory: '我为什么是一个文件.txt'
复制代码
2)使用try-except修改后:
- try:
- f = open('我为什么是一个文件.txt')
- print(f.read())
- f.close()
- except OSError:
- print('文件出错啦T_T')
- ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
- 文件出错啦T_T
复制代码
3)加上as reason将程序检测到出错的信息输出
- try:
- f = open('我为什么是一个文件.txt')
- print(f.read())
- f.close()
- except OSError as reason:
- print('文件出错啦T_T\n错误的原因是:'+ str(reason))
- ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
- 文件出错啦T_T
- 错误的原因是:[Errno 2] No such file or directory: '我为什么是一个文件.txt'
复制代码
4)增加多个except语句,提取代码段不同的异常问题
- try:
- sum = 1 + '1'
- f = open('我为什么是一个文件.txt')
- print(f.read())
- f.close()
- except OSError as reason:
- print('文件出错啦T_T\n错误的原因是:'+ str(reason))
- except TypeError as reason:
- print('类型出错啦T_T\n错误的原因是:'+ str(reason))
- ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
- 类型出错啦T_T
- 错误的原因是:unsupported operand type(s) for +: 'int' and 'str'
复制代码
5)如果except中为包含程序中存在的异常时,程序会直接爆出异常
- try:
- int('abc')
- sum = 1 + '1'
- f = open('我为什么是一个文件.txt')
- print(f.read())
- f.close()
- except OSError as reason:
- print('文件出错啦T_T\n错误的原因是:'+ str(reason))
- except TypeError as reason:
- print('类型出错啦T_T\n错误的原因是:'+ str(reason))
- ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
- Traceback (most recent call last):
- File "C:\Users\Administrator\Desktop\test.py", line 2, in <module>
- int('abc')
- ValueError: invalid literal for int() with base 10: 'abc'
复制代码
或者:
- try:
- sum = 1 + '1'
- f = open('我为什么是一个文件.txt')
- print(f.read())
- f.close()
- except (OSError, TypeError):
- print('出错啦T_T')
- >>>
- ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
- 出错啦T_T
复制代码
6)通用程序检测异常,并且报出异常,但是不详细(不推荐)
- try:
- int('abc')
- sum = 1 + '1'
- f = open('我为什么是一个文件.txt')
- print(f.read())
- f.close()
- except:
- print('出错啦T_T')
- ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
- 出错啦T_T
复制代码
2.try-finally
- finally:
- 无论如何都会被执行的代码
- try:
- f = open('我为什么是一个文件.txt', 'w')
- print(f.write('我存在了!'))
- sum = 1 + '1'
- except (OSError, TypeError):
- print('出错啦T_T')
- finally:
- f.close()
- ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
- 5
- 出错啦T_T
复制代码
3.raise语句
- >>> raise
- Traceback (most recent call last):
- File "<pyshell#0>", line 1, in <module>
- raise
- RuntimeError: No active exception to reraise
- >>> 1 / 0
- Traceback (most recent call last):
- File "<pyshell#1>", line 1, in <module>
- 1 / 0
- ZeroDivisionError: division by zero
- >>> raise ZeroDivisionError
- Traceback (most recent call last):
- File "<pyshell#2>", line 1, in <module>
- raise ZeroDivisionError
- ZeroDivisionError
- >>> raise ZeroDivisionError('除数为零的异常')
- Traceback (most recent call last):
- File "<pyshell#3>", line 1, in <module>
- raise ZeroDivisionError('除数为零的异常')
- ZeroDivisionError: 除数为零的异常
复制代码 |
评分
-
查看全部评分
|