首页 > 代码库 > Pyhon学习笔记4:异常

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:异常