"""演示三层调用 + 链式异常,并格式化 Traceback。""" from __future__ import annotations import traceback def load(data: dict[str, int]) -> int: try: return data["count"] except KeyError as exc: raise ValueError("missing count") from exc def compute(data: dict[str, int]) -> int: try: return int(load(data)) except ValueError as exc: raise TypeError("compute failed") from exc def formatted() -> str: try: compute({}) except TypeError as exc: return "".join(traceback.format_exception(exc)) raise AssertionError("expected TypeError") def main() -> None: print(formatted()) if __name__ == "__main__": main()