鱼C论坛

 找回密码
 立即注册
查看: 3671|回复: 0

[学习笔记] 033异常处理:你不可能总是对的2

[复制链接]
发表于 2017-7-9 22:53:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 人若有情死得早 于 2017-7-9 22:55 编辑

1.try-except语句
try:
检测范围
except Exception[as reason]:
出现异常(Exception)后的处理代码
1)代码:
  1. f = open('我为什么是一个文件.txt')
  2. print(f.read())
  3. f.close()
  4. ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
  5. Traceback (most recent call last):
  6.   File "C:\Users\Administrator\Desktop\test.py", line 1, in <module>
  7.     f = open('我为什么是一个文件.txt')
  8. FileNotFoundError: [Errno 2] No such file or directory: '我为什么是一个文件.txt'
复制代码

2)使用try-except修改后:
  1. try:
  2.     f = open('我为什么是一个文件.txt')
  3.     print(f.read())
  4.     f.close()
  5. except OSError:
  6. print('文件出错啦T_T')
  7. ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
  8. 文件出错啦T_T
复制代码

3)加上as reason将程序检测到出错的信息输出
  1. try:
  2.     f = open('我为什么是一个文件.txt')
  3.     print(f.read())
  4.     f.close()
  5. except OSError as reason:
  6. print('文件出错啦T_T\n错误的原因是:'+ str(reason))
  7. ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
  8. 文件出错啦T_T
  9. 错误的原因是:[Errno 2] No such file or directory: '我为什么是一个文件.txt'
复制代码

4)增加多个except语句,提取代码段不同的异常问题
  1. try:
  2.     sum = 1 + '1'
  3.     f = open('我为什么是一个文件.txt')
  4.     print(f.read())
  5.     f.close()
  6. except OSError as reason:
  7.     print('文件出错啦T_T\n错误的原因是:'+ str(reason))
  8. except TypeError as reason:
  9. print('类型出错啦T_T\n错误的原因是:'+ str(reason))
  10. ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
  11. 类型出错啦T_T
  12. 错误的原因是:unsupported operand type(s) for +: 'int' and 'str'
复制代码

5)如果except中为包含程序中存在的异常时,程序会直接爆出异常
  1. try:
  2.     int('abc')
  3.     sum = 1 + '1'
  4.     f = open('我为什么是一个文件.txt')
  5.     print(f.read())
  6.     f.close()
  7. except OSError as reason:
  8.     print('文件出错啦T_T\n错误的原因是:'+ str(reason))
  9. except TypeError as reason:
  10. print('类型出错啦T_T\n错误的原因是:'+ str(reason))
  11. ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
  12. Traceback (most recent call last):
  13.   File "C:\Users\Administrator\Desktop\test.py", line 2, in <module>
  14.     int('abc')
  15. ValueError: invalid literal for int() with base 10: 'abc'
复制代码

或者:
  1. try:
  2.     sum = 1 + '1'
  3.     f = open('我为什么是一个文件.txt')
  4.     print(f.read())
  5.     f.close()
  6. except (OSError, TypeError):
  7.     print('出错啦T_T')
  8. >>>
  9. ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
  10. 出错啦T_T
复制代码

6)通用程序检测异常,并且报出异常,但是不详细(不推荐)
  1. try:
  2.     int('abc')
  3.     sum = 1 + '1'
  4.     f = open('我为什么是一个文件.txt')
  5.     print(f.read())
  6.     f.close()
  7. except:
  8.     print('出错啦T_T')
  9. ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
  10. 出错啦T_T
复制代码

2.try-finally
  1. finally:
  2. 无论如何都会被执行的代码
  3. try:
  4.     f = open('我为什么是一个文件.txt', 'w')
  5.     print(f.write('我存在了!'))
  6.     sum = 1 + '1'
  7. except (OSError, TypeError):
  8.     print('出错啦T_T')
  9. finally:
  10.         f.close()
  11. ============== RESTART: C:\Users\Administrator\Desktop\test.py ==============
  12. 5
  13. 出错啦T_T
复制代码

3.raise语句
  1. >>> raise
  2. Traceback (most recent call last):
  3.   File "<pyshell#0>", line 1, in <module>
  4.     raise
  5. RuntimeError: No active exception to reraise
  6. >>> 1 / 0
  7. Traceback (most recent call last):
  8.   File "<pyshell#1>", line 1, in <module>
  9.     1 / 0
  10. ZeroDivisionError: division by zero
  11. >>> raise ZeroDivisionError
  12. Traceback (most recent call last):
  13.   File "<pyshell#2>", line 1, in <module>
  14.     raise ZeroDivisionError
  15. ZeroDivisionError
  16. >>> raise ZeroDivisionError('除数为零的异常')
  17. Traceback (most recent call last):
  18.   File "<pyshell#3>", line 1, in <module>
  19.     raise ZeroDivisionError('除数为零的异常')
  20. ZeroDivisionError: 除数为零的异常
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
小甲鱼 + 5 + 5 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-4 07:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表