Skip to content

错误处理

Error handling in Python is managed through the use of try/except/finally

Python has numerous built-in exceptions. When creating except blocks, they need to be created from most specific to most generic according to the hierarchy.

x = 42
y = 0
try:
    print(x / y)
except ZeroDivisionError as e:
    # Optionally, log e somewhere
    print('Sorry, something went wrong')
except:
    print('Something really went wrong')
finally:
    print('This always runs on success or failure')