day 1 异常基本功能

时间:2023-03-10 02:09:10
day 1 异常基本功能

1.什么是异常?程序出现的错误

In [1]: open('xxx.txt')
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-1-e41727968802> in <module>()
----> 1 open('xxx.txt') FileNotFoundError: [Errno 2] No such file or directory: 'xxx.txt' In [2]: print(num)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-64c1b968c91a> in <module>()
----> 1 print(num) NameError: name 'num' is not defined

2.异常处理,预处理方案

try:
print(num)
print("-----1-----")
except NameError:
print("出现名字异常错误")
print("------2------")
#### 运行结果
出现名字异常错误
------2------

3.多个异常

  1)版本1:

 try:
open("xxx.txt")
print(num)
print("-----1-----")
except NameError:
print("出现名字异常错误") except FileNotFoundError:
print("文件不存在。。") print("------2------")
文件不存在。。
------2------

  2)版本2:

 try:
open("xxx.txt")
print(num)
print("-----1-----")
except (NameError,FileNotFoundError):
print("出现异常错误") print("------2------")
出现异常错误
------2------

  3)版本3:其他异常

try:
11/0
open("xxx.txt")
print(num)
print("-----1-----")
except (NameError,FileNotFoundError):
print("出现异常错误")
except Exception:
print("如果用了Exception,那么意味着只要上面的except没有捕获到异常,这个except一定会捕获到") print("------2------")
如果用了Exception,那么意味着只要上面的except没有捕获到异常,这个except一定会捕获到
------2------

  4)版本4:查看异常原因

try:
11/0
open("xxx.txt")
print(num)
print("-----1-----")
except (NameError,FileNotFoundError):
print("出现异常错误") except Exception as ret:
print("如果用了Exception,那么意味着只要上面的except没有捕获到异常,这个except一定会捕获到")
print(ret) print("------2------")
如果用了Exception,那么意味着只要上面的except没有捕获到异常,这个except一定会捕获到
division by zero
------2------

  5)版本5:没有异常

try:
# 11/0
# open("xxx.txt")
# print(num)
print("-----1-----")
except (NameError,FileNotFoundError):
print("出现异常错误") except Exception as ret:
print("如果用了Exception,那么意味着只要上面的except没有捕获到异常,这个except一定会捕获到")
print(ret) else:
print("没有出现异常,,,哈哈哈哈") print("------2------")
-----1-----
没有出现异常,,,哈哈哈哈
------2------

4  finally

  1)没有异常

try:
# 11/0
# open("xxx.txt")
# print(num)
print("-----1-----")
except (NameError,FileNotFoundError):
print("出现异常错误") except Exception as ret:
print("如果用了Exception,那么意味着只要上面的except没有捕获到异常,这个except一定会捕获到")
print(ret) else:
print("没有出现异常,,,哈哈哈哈")
finally:
print("---这段程序死亡时候执行finally---") print("------2------")
-----1-----
没有出现异常,,,哈哈哈哈
---这段程序死亡时候执行finally---
------2------

  2)有异常

try:
11/0
open("xxx.txt")
print(num)
print("-----1-----")
except (NameError,FileNotFoundError):
print("出现异常错误") except Exception as ret:
print("如果用了Exception,那么意味着只要上面的except没有捕获到异常,这个except一定会捕获到")
print(ret) else:
print("没有出现异常,,,哈哈哈哈")
finally:
print("---这段程序死亡时候执行finally---") print("------2------")
如果用了Exception,那么意味着只要上面的except没有捕获到异常,这个except一定会捕获到
division by zero
---这段程序死亡时候执行finally---
------2------

  

  3)不管有没有异常,都关闭文件

import time
try:
f = open('test.txt')
try:
while True:
content = f.readline()
if len(content) == 0:
break
time.sleep(2)
print(content)
except:
#如果在读取文件的过程中,产生了异常,那么就会捕获到
#比如 按下了 ctrl+c
pass
finally:
f.close()
print('关闭文件')
except:
print("没有这个文件")