主动触发异常raise

try:
    print("123")
    raise Exception("出错了")  # 往Exception这个类中传了一个字段(“出错了”)
except Exception as e:         # 封装了含错误信息的对象
    print(e)
print("程序结束")

 其实是Exception 中封装了__str__的一个方法。

class Foo:

    def __init__(self, arg):
        self.xo = arg

    def __str__(self):
        return self.xo


obj = Foo("出错了...")
print(obj)
print(str(obj))

 

posted @ 2018-10-14 21:38  xuwenwei  阅读(229)  评论(0)    收藏  举报