python - 不提供异常的错误代码?
问题描述
def main():
try:
exit_code = 0
# Some code that might raise an exception
except FileNotFoundError as error:
exit_code = error.errno
except (ValueError, TypeError) as error:
print(error)
finally:
sys.exit(exit_code)
if __name__ == "__main__":
main()
ValueError
并且TypeError
不要提供错误代码,因为 pylint 给了我以下错误:
Instance of 'ValueError' has no 'errno' memberpylint(no-member)
Instance of 'TypeError' has no 'errno' memberpylint(no-member)
当我想退出脚本并将错误代码提供给 时sys.exit()
,应该为不返回错误代码的异常提供什么值?
解决方案
您提供什么退出状态取决于您。
如果程序成功完成,则返回 0(有时称为 EXIT_SUCCESS)被广泛接受。失败时返回 1 (EXIT_FAILURE) 也很常见。
推荐阅读
- node.js - 无法从 'index.js' 中找到模块 'react' - (node_modules/jest-resolve/build/index.js:259:17)
- node.js - 将 gltf-pipeline 包含到浏览器中
- laravel - 试图在laravel7中获取非对象的属性“用户组”
- firebase - 谷歌云函数中实时数据库触发器的 setTimeout()
- python - 无法使用套接字在 python 中通过 Internet 发送数据
- python - Oracle 10g 速成版与 Python 中的 Mysql
- python - CoreText 注意:运行 Tkinter 和 Python 时 macOS 上的警告
- r - 基于两列将长格式数据框重塑为宽
- c# - C# 使用 HttpWebRequest 通过代理调用 API
- javascript - 为什么使用“Vue.prototype.$echarts = echarts”