1.文件的異常處理:try except FileNotFoundError
成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),水富網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:水富等地區(qū)。水富做網(wǎng)站價(jià)格咨詢: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="異常測(cè)試"
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("我都會(huì)執(zhí)行!")
6.拋出異常