Pthon面向对象-异常处理

              Pthon面向对象-异常处理

                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.异常概述

1>.错误(Error)

  逻辑错误:
    算法写错了,例如加法写成了减法。

  笔误:
    例如变量名写错了,语法错误等。

  函数或类使用错误,其实这也属于逻辑错误。

  总之,错误时可以避免的。

2>.异常(Exception)

  Exception本意就是意外情况。

  这有个前提,没有出现上面说的错误,也就是说程序写的没有问题,但是在某种情况下,会出现一些意外,导致程序无法正常的执行下去。

  例如open函数操作一个文件,文件不村咋子,或者创建一个文件时已经存在了,或者访问一个网络文件,突然断网了,这就是异常,是个意外的情况。

  异常不可能避免。

3>.错误和异常

  在高级编程这语言中,一般都有错误和异常的概念,异常时可以捕获,并被处理的,但是错误是不能被捕获的。

  一个健壮的程序,尽可能的避免错误,尽可能的避免错误,尽可能的捕获,处理各种异常。

4>.产生异常

 1 #!/usr/bin/env python
 2 #_*_conding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 
 6 """
 7     raise语句显式的抛出异常,Python解释器自己检测到异常并引发它
 8 """
 9 def foo():
10     print("before")
11     raise Exception("my exception")  # raise主动抛出异常,程序会在异常抛出的地方中断执行,如果不捕获,就会提前结束程序(其实是终止当前线程的执行)
12     print("after")
13 
14 foo()

5>.异常的捕获

 1 #!/usr/bin/env python
 2 #_*_conding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 
 6 def foo():
 7     try:
 8         print("before")
 9         c = 1 / 0                #想必大家都知道在运行改行代码会引发"除零异常"
10         print("after")
11     except:                      #我们可以指定捕获的异常类型,如果不写默认捕获所有异常。
12         print("error")
13 
14     print("catch the exception")
15 
16 foo()
17 
18 print("{0} 程序运行结束 {0}".format("*" * 20))
19 
20 
21 
22 #以上代码执行结果如下:
23 before
24 error
25 catch the exception
26 ******************** 程序运行结束 ********************

二.异常类及继承层次

1>.异常的祖先类(BaseException)

2>.通过"__subclasses__()"属性查看BaseException的子类

 1 #!/usr/bin/env python
 2 #_*_conding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 
 6 print(BaseException.__subclasses__())
 7 
 8 print(Exception.__subclasses__())
 9 
10 print(RuntimeError.__subclasses__())
11 
12 print(ArithmeticError.__subclasses__())
13 
14 print(LookupError.__subclasses__())
15 
16 print(OSError.__subclasses__())
17 
18 
19 
20 
21 #以上代码执行结果如下:
22 [<class 'Exception'>, <class 'GeneratorExit'>, <class 'SystemExit'>, <class 'KeyboardInterrupt'>]
23 [<class 'TypeError'>, <class 'StopAsyncIteration'>, <class 'StopIteration'>, <class 'ImportError'>, <class 'OSError'>, <class 'EOFError'>, <class 'RuntimeError'>, <class 'NameError'>, <class 'AttributeError'>, <class 'SyntaxError'>, <class 'LookupError'>, <class 'ValueError'>, <class 'AssertionError'>, <class 'ArithmeticError'>, <class 'SystemError'>, <class 'ReferenceError'>, <class 'MemoryError'>, <class 'BufferError'>, <class 'Warning'>, <class 'locale.Error'>]
24 [<class 'RecursionError'>, <class 'NotImplementedError'>, <class '_frozen_importlib._DeadlockError'>]
25 [<class 'FloatingPointError'>, <class 'OverflowError'>, <class 'ZeroDivisionError'>]
26 [<class 'IndexError'>, <class 'KeyError'>, <class 'encodings.CodecRegistryError'>]
27 [<class 'ConnectionError'>, <class 'BlockingIOError'>, <class 'ChildProcessError'>, <class 'FileExistsError'>, <class 'FileNotFoundError'>, <class 'IsADirectoryError'>, <class 'NotADirectoryError'>, <class 'InterruptedError'>, <class 'PermissionError'>, <class 'ProcessLookupError'>, <class 'TimeoutError'>, <class 'io.UnsupportedOperation'>]

三.自定义异常类

 1 #!/usr/bin/env python
 2 #_*_conding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 
 6 class MyException(Exception):
 7     pass
 8 
 9 try:
10     raise MyException()
11 except MyException:     #捕获自定义异常
12     print("catch the exception")
13 
14 
15 
16 #以上代码执行结果如下:
17 catch the exception

 

四.多种捕获

 1 #!/usr/bin/env python
 2 #_*_conding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 
 6 import sys
 7 
 8 """
 9     捕获规则:
10         捕获是从上到下依次比较,如果匹配,则执行匹配的except语句块
11         如果被一个except语句捕获,其它except语句就不会再次捕获了
12         如果没有任何一个except语句捕获到这个异常,该异常向外抛出
13         如果"except:"称为缺省捕获,缺省捕获必须except捕获语句的最后。
14         
15     捕获的原则:
16         从小到大,从具体到宽泛
17 """
18 
19 class MyException(Exception):
20     pass
21 
22 try:
23     a = 1 / 0
24     raise MyException()
25     open("t")
26     sys.exit(1)
27 except ZeroDivisionError:
28     print("zero")
29 except ArithmeticError:
30     print("ari")
31 except MyException:     #捕获自定义异常
32     print("catch the exception")
33 except Exception:
34     print("excption")
35 except:                 #缺省捕获
36     print("sys exit")
37 
38 
39 
40 
41 #以上代码执行几个如下:
42 zero

五.as子句

 1 #!/usr/bin/env python
 2 #_*_conding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 
 6 
 7 class MyException(Exception):
 8     def __init__(self,code,message):
 9         self.code = code
10         self.message = message
11 
12 try:
13     """
14         raise语句:
15             raise后要求应该是BaseException类的子类或实例,如果是类,将被无参实例化。
16             raise后上面都没有,表示抛出最近一个被激活的异常,如果没有被激活的异常,则抛出类型异常。这种方式很少用。
17     """
18     raise MyException(200,"ok")
19 except MyException as e:
20     print("catch my exception: {} {}".format(e.code,e.message))
21 except Exception as e:
22     print("{}".format(e))
23 
24 
25 
26 
27 #以上代码执行几个如下:
28 catch my exception: 200 ok

六.finally子句

 1 #!/usr/bin/env python
 2 #_*_conding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 
 6 f = None
 7 
 8 try:
 9     f  = open("test.txt")
10 except FileNotFoundError as e:
11     print("{} {} {}".format(e.__class__,e.errno,e.strerror))
12 finally:
13     print("清理工作")
14     f.close()

七.else子句

 1 #!/usr/bin/env python
 2 #_*_conding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie
 5 
 6 try:
 7     ret = 1 * 0
 8 except ArithmeticError as e:
 9     print(e)
10 else:                       #没有任何异常则执行
11     print("ok")
12 finally:
13     print("finally")
14     
15     
16     
17 #以上代码执行结果如下:
18 ok
19 finally

八.总结

1>.语法格式

try:
  <语句>    #运行别的代码
except <异常类>:
  <语句>    #捕获某种类型的异常
except <异常类> as <变量名>:
  <语句>    #捕获某种类的异常并获得对象
except:
  <语句>    #缺省捕获
else:
  <语句>    #如果没有异常发生才会执行
finally:
  <语句>    #退出try时总会执行

2>.try的工作原理

  如果try中语句执行时发生异常,搜索except子句,并执行第一个匹配该异常的except子句。
  
  如果try中语句执行发生异常,却没有匹配的except子句,异常将被递交到外层的try,如果外层不处理这个异常,异常将继续向外层传递。如果都不处理该异常,则会传递到最外层,如果还没有处理,就终止异常所在的线程。

  如果在try执行时没有发生异常,如有else子句,可执行else子句中的语句。

  无论try中是否发生异常,finally子句最终都会执行。
原文地址:https://www.cnblogs.com/yinzhengjie/p/11192651.html