raise + 自定义Exception
场景
退出多重for循环,break只能退出一层
class ReturnError(Exception): pass def compare(dst, src): try: for dst_k, dst_v in dst_dict.items(): if dst_k not in src_dict: raise ReturnError(msg) else: for dst_v_k, dst_v_v in dst_v.items(): if dst_v_v != src_dict[dst_k][dst_v_k]: raise ReturnError(msg) except ReturnError as e: e except Exception as e: _e1 = str(traceback.format_exc()) e, _e1 finally: return flag, msg
开源永流传