主动触发异常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))
原文地址:https://www.cnblogs.com/xuwenwei/p/9788159.html