python 异常

记录关于python相关的知识。

自定义异常:

继承 Exception 类,然后重写__int__和__str__方法。

class Error(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)

 

发现错误,引发异常:

 raise Error("find error")

 

捕获异常:

try:
    raise Error("find error")
except Error as e:
    print 'My exception occurred, value:', e.value

 

posted @ 2017-12-29 13:41  chenhuan001  阅读(132)  评论(0编辑  收藏  举报