Pyhon学习笔记4:异常
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pyhon学习笔记4:异常相关的知识,希望对你有一定的参考价值。
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.抛出异常
以上是关于Pyhon学习笔记4:异常的主要内容,如果未能解决你的问题,请参考以下文章