Python 失败后继续执行脚本

标签 python bitbucket-server

我正在为 Atlassian 的 stash 应用程序使用 python api。

脚本无法删除项目,因为它里面有存储库,然后退出。

Traceback (most recent call last):
  File "stash.py", line 38, in <module>
    stash.projects[p["key"]].delete()
  File "<string>", line 2, in delete
  File "/usr/local/lib/python2.7/site-packages/stashy/errors.py", line 46, in ok_or_error
    maybe_throw(response)
  File "/usr/local/lib/python2.7/site-packages/stashy/errors.py", line 40, in maybe_throw
    raise e
stashy.errors.GenericException: 409: {u'errors': [{u'message': u'The project "TEST" cannot be deleted because it has repositories.', u'exceptionName': u'com.atlassian.bitbucket.IntegrityException', u'context': None}]}

是否可以显示错误但继续执行脚本?

最佳答案

在 Python 中,这称为 try ..expect 模式或异常处理

最简单的形式:

try:
    stash.projects[p["key"]].delete()
except Exception as e:
    print(e)

更多信息:

关于Python 失败后继续执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33004835/

相关文章:

git - 迁移 Git 主机

shell - 在 Ansible 中启动 Bitbucket 服务器

python - 如何告诉 Python 在 pylab.show() 之后结束?

python - 从列表中消除重复项的更快方法

python - 在 python 中使用 for 循环反转字符串 IndexError : string index out of range

.net - 保护 git 存储库中的文件

eclipse - Atlassian 插件 : unknown packaging type

git - 在 Atlassian Stash 中查看分支 TreeView

python - PyQt widget可以存储在局部变量中吗?

python - 如何在 numba.njit 中进行离散傅立叶变换 (FFT)?