捕获与抛出
try: int("abc") except ValueError as e: print("转换失败", e) def must_positive(n: int): if n <= 0: raise ValueError("n must be positive")
断言
def divide(a, b): assert b != 0, "b cannot be zero" return a / b
转载请注明:周志洋的博客 » Python基础知识-错误与异常
try: int("abc") except ValueError as e: print("转换失败", e) def must_positive(n: int): if n <= 0: raise ValueError("n must be positive")
def divide(a, b): assert b != 0, "b cannot be zero" return a / b
转载请注明:周志洋的博客 » Python基础知识-错误与异常