python 异常

记录关于python相关的知识。python

自定义异常:spa

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

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

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

 

发现错误,引起异常:blog

 raise Error("find error")

 

捕获异常:继承

try:
    raise Error("find error")
except Error as e:
    print 'My exception occurred, value:', e.value
相关文章
相关标签/搜索