1.文件的异常处理:try except FileNotFoundError
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.抛出异常
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。