Pyhon学习笔记4:异常-创新互联
1.文件的异常处理:try except FileNotFoundError
成都创新互联公司自2013年起,是专业互联网技术服务公司,拥有项目成都做网站、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元武山做网站,已为上家服务,为武山各地企业和个人服务,联系电话:18982081108try:
open("abc.txt",'r')
except FileNotFoundError:
print("异常了!")
2.name的异常 try except NameError
try :
print(aa)
except NameError:
print("异常了!")
3.可以使用BaseException类处理所以的异常
try:
open("abc.txt",'r')
print(aa)
except BaseException:
print("异常了!")
4.打印异常日志
try:
# open("abc.txt",'r')
print(aa)
except BaseException as msg:
print("异常了!")
print(msg)
5.python中常见的异常:
(1)try ...else..
try:
aa="异常测试"
print(aa)
except Exception as msg:
print(msg)
else:
print("没有异常!")
(2)try ...finally..
try:
# open("abc.txt",'r')
print(aa)
except BaseException as msg:
print("异常了!")
print(msg)
finally:
print("我都会执行!")
6.抛出异常
分享标题:Pyhon学习笔记4:异常-创新互联
URL标题:http://azwzsj.com/article/cddhpp.html