面向对象---自定义异常类型
1 class MyException(BaseException): 2 def __init__(self, msg): 3 super(MyException, self).__init__() 4 self.msg = msg 5 6 def __str__(self): 7 return '<%s>' % self.msg 8 9 10 raise MyException('我自己的异常类型') 11 12 结果为: 13 14 Traceback (most recent call last): 15 File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/33 try...except详细用法.py", line 84, in <module> 16 raise MyException('我自己的异常类型') 17 __main__.MyException: <我自己的异常类型>
定制异常类型成功