Python之异常处理 Author: LionLpeoridae Date: December 21, 2024 1:24:59 Category: 头歌实训 第1关:Python异常类与自定义异常12345678910111213141516# -*- coding: utf-8 -*-class MyError(Exception): #********begin*********# id = 0 def __init__(self, id): Exception.__init__(self) self.id = id def __str__(self): return f"这是我定义的第{self.id}个异常" #******** end*********# 第2关:Python中的异常处理结构123456789101112131415161718192021class MyError(Exception): def __str__(self): return "长度过长,大于3"def TestLength(x): x = len(x) # *********begin*********# try: if x > 3: raise MyError(x) except MyError as e: print(e) else: print("长度合适") finally: print("执行完毕") # ********* end*********# Author: LionLpeoridae Slogan: 狮兔 Tag(s): # 头哥 back · home 数据结构 Python之多线程编程