【Python】try catch 或者 try except

2024-08-19  本文已影响0人  盐果儿

try catch 是 Java 里的,try except 是 Python 里的。

try... else... finally...

try:

  result = 10 / 2

except ZeroDivisionError:

  print("Cannot divide by zero.")

else:

  print("Division successful!")

finally:

  print("This block always executes.")

捕获所有异常:

try:

  ...

except Exception as e:

  ...

多个except异常:

try:

  ...

except ValueError as ve:

  ...

except ZeroDivisionError as zde:

  ...

上一篇 下一篇

猜你喜欢

热点阅读