Python exceptions All In One
Different exceptions are raised for different reasons.
Common exceptions:
ImportError: an import fails;IndexError: a list is indexed with an out-of-range number;NameError: an unknown variable is used;SyntaxError: the code can't be parsed properly;TypeError: a function is called on a value of an inappropriate type;ValueError: a function is called on a value of the correct type, but with an inappropriate value.
Python has several other built-in exceptions, such as ZeroDivisionError and OSError.
Third-party libraries also often define their own exceptions.
try:
num1 = 7
num2 = 0
num3 = 2
print (num1 / num3)
print("✅ Done calculation")
print (num1 / num2)
except ZeroDivisionError:
print("❌ An error occurred, due to zero division")
"""
$ py3 ./exception-handling-try-except.py
3.5
✅ Done calculation
❌ An error occurred, due to zero division
"""

demos
(? 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 ?️,侵权必究⚠️!