1.文件的异常处理:try except FileNotFoundError
成都创新互联是一家专注于网站建设、网站设计与策划设计,水富网站建设哪家好?成都创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:水富等地区。水富做网站价格咨询:13518219792
try:
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.抛出异常