错误和异常#
异常指在程序运行过程中发生的异常事件,通常是由外部问题(如硬件错误、输入错误)所导致的。
常见异常#
SyntaxError#
IndentationError#
NameError#
AttributeError#
KeyError#
TypeError#
ValueError#
ImportError#
ModuleNotFoundError#
AssertError#
异常处理#
Python中用try-except语句处理异常,语法格式为
try:
<statement>
except <Exception> as e:
<other statement>
举个例子: