个人博客

Python异常处理

异常嵌套时注意的问题

Python中类的归一化设计

利用Python的traceback模块显示与记录异常

自定义异常

class MY(BaseException):
    def __init__(self,msg):
        super().__init__()
        self.msg = msg

    def __str__(self):
        return f"{self.msg}"

try:
    print(555555)
    raise MY("666")
except MY as e:  # 自定义异常需要自己捕获!!!
    print(e)
    import traceback
    print(traceback.format_exc())
except Exception as e:
    print(e)
# else:
#     print("------------")